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

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

 ?>

最佳答案

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

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


有关更多信息,https://codex.wordpress.org/Class_Reference/WP_Query

09-19 22:52