在同一页面Wordpress上两次短代码

时间:2016-12-29 00:51:26

标签: php jquery wordpress shortcode

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

<?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 ) ;

    $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'];


    ?>

     <script>

            var tQ = jQuery.noConflict();

        tQ(document).ready(function(){

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

                var testimonialItem = tQ('#testimonials-slider-shortcode 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 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 .item').each(function(){  

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

                    testimonialBoxShort = tQ(this).height();  

                }

            });    

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

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

        }); 

    </script>

    <div id="testimonials-slider-shortcode" 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" 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" 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
}

add_shortcode('testimonials', 'eleva_shortcode');

 ?>

2 个答案:

答案 0 :(得分:0)

循环结束后,您需要重置发布数据。

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

了解更多信息https://codex.wordpress.org/Class_Reference/WP_Query

答案 1 :(得分:0)

我的猜测是问题正在发生,因为有两个具有相同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');

 ?>