Php 在同一页Wordpress上短码两次

Php 在同一页Wordpress上短码两次,php,jquery,wordpress,shortcode,Php,Jquery,Wordpress,Shortcode,我试图在同一页上添加两个短代码,但问题是第二个短代码没有显示任何内容。我认为这个查询可能有问题,但我不确定这一点。这是我的短代码: var tQ=jQuery.noConflict(); tQ(文档).ready(函数(){ if(tQ(窗口).width()>992){ var-CertificationItem=tQ(“#Certifications滑块短码div[class*=“col md-”); 对于(变量i=0;i992){ var-CertificationItem=tQ(“#C

我试图在同一页上添加两个短代码,但问题是第二个短代码没有显示任何内容。我认为这个查询可能有问题,但我不确定这一点。这是我的短代码:


var tQ=jQuery.noConflict();
tQ(文档).ready(函数(){
if(tQ(窗口).width()>992){
var-CertificationItem=tQ(“#Certifications滑块短码div[class*=“col md-”);
对于(变量i=0;iboxshort){
testionalboxshort=tQ(this).height();
}
});    
tQ(“#鉴定滑块短码.item”)。高度(鉴定框短);
tQ('#推荐滑块短码.item').first().addClass('active');
}); 

循环结束后,需要重置post数据

/* Restore original Post Data */     `wp_reset_postdata();`

有关更多信息

我猜问题是因为有两个具有相同ID属性的HTML元素

尝试下面的代码,它使用PHP的
uniqid
函数为每个滑块元素创建唯一的ID

<?php 

defined( 'ABSPATH' ) or die( 'No script kiddies please!' );

if ( ! is_admin() ) {
        require_once( ABSPATH . 'wp-admin/includes/post.php' );
}

function eleva_shortcode($atts = array()){

    $atts=shortcode_atts( array(

        'display' => '3', 
        'limit' => '5',
        'length' => '15',
        'title'  => "",
        'category' => ""
    ), $atts ) ;

    $el_id = uniqid();

    $display = $atts['display'] ;
    $pars = intval($display);
    $col = 0;

    if($pars==1 || $pars==2 || $pars==3 || $pars==4 || $pars==6){
        $col = 12 / $pars;
    }

    else{

        $pars =3;

        $col = 12 / $pars;

    } 

    $limit = $atts['limit'];

    $pars_limit = intval($limit);

    $length = $atts['length'];

    $pars_length = intval($length);

    $title = $atts['title'];

    $id = post_exists($title);

    $taxonomy = $atts['category'];

    ob_start();
    ?>

     <script>

            var tQ = jQuery.noConflict(),
                elId = '<?php echo $el_id; ?>';

        tQ(document).ready(function(){

            if (tQ(window).width() > 992) {

                var testimonialItem = tQ('#testimonials-slider-shortcode-' + elId + ' div[class*="col-md-"]');

                for(var i = 0; i < testimonialItem.length; i+=<?php echo $pars;?>) {

                    testimonialItem.slice(i, i+<?php echo $pars;?>).wrapAll('<div class="item"></div>');

                }

            } else{

                var testimonialItem = tQ('#testimonials-slider-shortcode-' + elId + ' div[class*="col-md-"]');

                for(var i = 0; i < testimonialItem.length; i+=1) {

                    testimonialItem.slice(i, i+1).wrapAll('<div class="item"></div>');

                }

            }

            var testimonialBoxShort = 0;

            tQ('#testimonials-slider-shortcode-' + elId + ' .item').each(function(){  

                if(tQ(this).height() > testimonialBoxShort){  

                    testimonialBoxShort = tQ(this).height();  

                }

            });    

            tQ('#testimonials-slider-shortcode-' + elId + ' .item').height(testimonialBoxShort);

            tQ('#testimonials-slider-shortcode-' + elId + ' .item').first().addClass('active');

        }); 

    </script>

    <div id="testimonials-slider-shortcode-<?php echo $el_id; ?>" class="carousel slide" data-ride="carousel">

        <ol class="carousel-indicators"></ol>

        <div class="carousel-inner" role="listbox">

        <?php



        if($id == 0 && empty($taxonomy) ){

            $queryT = new WP_Query( array( 'post_type' => 'testimonials') );

        }elseif ($id == 0 && !empty($taxonomy)){

              $queryT = new WP_Query( array( 'post_type' => 'testimonials', 'tax_query' => array(
                          array(
                           'taxonomy' => 'categorytestimonials',
                           'field'    => 'slug',
                           'terms'    => $taxonomy,
                          )) )); //p is for the id;

        }elseif ($id != 0 && empty($taxonomy)){

              $queryT = new WP_Query( array( 'post_type' => 'testimonials', 'p' => $id) ); //p is for the id;

        }else{

              $queryT = new WP_Query( array( 'post_type' => 'testimonials', 'p' => $id, 'tax_query' => array(
                          array(
                           'taxonomy' => 'categorytestimonials',
                           'field'    => 'slug',
                           'terms'    => $taxonomy,
                          )) )); //p is for the id;) ); //p is for the id;

        }     

        if ( $queryT->have_posts() ) : ?>

            <!-- the loop -->

            <?php $count = 0;

            while ( $queryT->have_posts()) : $queryT->the_post(); ?>

            <?php   //variables

                $author = get_post_meta( get_the_ID(), 'author', true ); 

                $link = get_permalink();

            ?>

            <?php if($count < $pars_limit || $pars_limit==0): ?>

            <div class="col-md-<?php echo $col;?>">

                <div class="img-wrap">

                    <?php if ( has_post_thumbnail() ) : // check if the post has a Post Thumbnail assigned to it. ?>

                        <a href="<?php echo $link;?>">

                            <?php the_post_thumbnail('full');?>

                        </a>

                    <?php else : ?>

                        <a href="<?php echo $link;?>">

                            <img alt="<?php bloginfo('name'); ?>" src="<?php echo get_template_directory_uri(); ?>/images/default.jpg">

                        </a>

                    <?php endif; ?>

                </div>

                <div class="info-wrap">

                    <p><?php echo eleva_testimonials_excerpt($pars_length +1); ?></p>

                    <?php // Check if the custom field has a value.

                        if ( ! empty( $author ) ) {

                            echo '<span><a href="'. $link .'">'. $author . '</a></span>';

                        }

                    ?>  

                </div>

            </div>

            <?php endif;

                  $count= $count +1;?>

            <?php endwhile; ?>

            <!-- end of the loop -->

            <?php wp_reset_postdata(); ?>

        </div>

        <a class="left carousel-control" href="#testimonials-slider-shortcode-<?php echo $el_id; ?>" role="button" data-slide="prev">

            <span class="glyphicon glyphicon-chevron-left fa fa-angle-left" aria-hidden="true"></span>

            <span class="sr-only">Previous</span>

        </a>

        <a class="right carousel-control" href="#testimonials-slider-shortcode-<?php echo $el_id; ?>" role="button" data-slide="next">

            <span class="glyphicon glyphicon-chevron-right fa fa-angle-right" aria-hidden="true"></span>

            <span class="sr-only">Next</span>

        </a>

        <?php else : ?>

            <p><?php _e( 'Sorry, no testimonials at this time' ); ?></p>

        <?php endif; ?>

            </div> 
       <?php
   return ob_get_clean();
}

add_shortcode('testimonials', 'eleva_shortcode');

 ?>

var tQ=jQuery.noConflict(),
elId=“”;
tQ(文档).ready(函数(){
if(tQ(窗口).width()>992){
var-CertificationItem=tQ(“#Certifications滑块短码-”+elId+”div[class*=“col-md-”]);
对于(变量i=0;iboxshort){
testionalboxshort=tQ(this).height();
}
});    
tQ(“#鉴定滑块短码-”+elId+“.item”)。高度(鉴定框短);
tQ(“#推荐滑块短码-”+elId+“.item”).first().addClass('active');
}); 

我在结束循环后这样做了:看,这太棒了!谢谢我能问你点别的吗?短代码总是出现在页面顶部。。我如何解决这个问题?@manuelsilva这是因为我们正在打印输出,而不是从函数返回输出,现在检查更新的代码,并请将其作为答案接受。谢谢,它工作得很好。。有办法将其作为答案接受吗??再次感谢你