为什么这个查询在另一个存在时不起作用?

Why is this query not working when another is present?

这让我抓狂,我敢肯定这是愚蠢的事情,但我太着急了,找不到它。我正在构建一个自定义 WP 主题,它有 2 个滑块,一个是默认的 Bootstrap 滑块,另一个是 Owl 滑块。在你告诉我之前 "why don't you use 2 instances of Owl slider?",我已经想到了,经过测试并且有效。但是,我很好奇为什么这不起作用。这是我的代码:

BOOTSTRAP 轮播:

<section id="panel4">
  <div class="container">
    <h2>Testimonials</h2>
  </div>
  <!-- container -->
  <div class="container-fluid">
    <?php $queryObject=new WP_Query( 'post_type=testimonial' ); // The Loop! if ($queryObject->have_posts()) { ?>
    <div id="carousel-testimonial" class="carousel slide" data-ride="carousel">
      <!-- Wrapper for slides -->
      <div class="carousel-inner" role="listbox">
        <?php while ($queryObject->have_posts()) {$queryObject->the_post(); ?>
        <div class="item ">
          <div class="container">
            <div class="row">
              <div class="col-sm-3 col-md-2 thumb-box">
                <img class="thumb" src="<?php echo types_render_field( " testimonial-avatar ", array( url => "true ", "width " => "400 ", "height " => "300 ", "proportional " => "true " ) );?>">
              </div>
              <div class="col-sm-9 col-md-10">
                <h3>"<?php echo types_render_field("testimonial-excerpt", array("argument1"=>"value1","argument2"=>"value2","argument2"=>"value2"));    ?>"</h3>

                <blockquote>
                  <?php echo types_render_field( "testimonial-text", array( "argument1"=>"value1","argument2"=>"value2","argument2"=>"value2")); ?></blockquote>
                <p class="sig">
                  <?php echo types_render_field( "testimonial-name", array( "argument1"=>"value1","argument2"=>"value2","argument2"=>"value2")); ?></p>
              </div>
            </div>
          </div>
        </div>
        <?php } ?>
      </div>
      <?php } ?>
      <!-- Controls -->
      <a class="left carousel-control" href="#carousel-testimonial" role="button" data-slide="prev">
        <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
        <span class="sr-only">Previous</span>
      </a>
      <a class="right carousel-control" href="#carousel-testimonial" role="button" data-slide="next">
        <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
        <span class="sr-only">Next</span>
      </a>

    </div>
    <!-- .carousel -->
  </div>
  <!-- .container -->
</section>

然后是 OWL 滑块

<?php if( have_rows( 'slider_photos') ): ?>

<div class="container-fluid">
  <div id="happy" class="owl-carousel">

    <?php while( have_rows( 'slider_photos') ): the_row(); // vars $image=g et_sub_field( 'slider_image'); ?>
    <div class="item-owl">
      <img class="img-responsive" src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt'] ?>" />
    </div>

    <?php endwhile; ?>

  </div>
  <!-- .carousel -->
</div>

<?php endif; ?>

现在,问题似乎出在Bootstrap。如果我将其取下,Owl 滑块会起作用。如果我添加 2 个 Owl Slider 实例,它就可以工作。如果我添加 Bootstrap 旋转木马,只有 Bootstrap 有效而 Owl 什么也不显示(甚至在代码中也不显示,就好像它根本不存在一样)

谁能告诉我这是怎么回事以及如何解决这个问题?

您很可能需要在 Owl 滑块 中的 have_rows( 'slider_photos') 之前调用 wp_reset_postdata(),以恢复全局 $post对象,您在 Bootstrap 轮播 循环中覆盖。

have_rows('slider_photos') 依赖于全局 $post 对象。

另一种选择是使用 have_rows( 'slider_photos', $post_id ) 指定要使用的 $post_id