【问题标题】:first post in while loop of wordpresswordpress 的 while 循环中的第一篇文章
【发布时间】:2018-04-03 04:25:50
【问题描述】:

我正在尝试使用高级自定义字段在 WordPress 中创建引导轮播。

循环中的第一个“carousel-item”必须具有“active”类。我无法弄清楚如何定义 if 条件,以便将类添加到循环的第一次迭代中。

轮播指示器也是如此。 class active 应该添加到第一次迭代中,并且 data-slide-to="x" 应该是循环的计数器。知道如何让计数和类正常工作吗?

<section>
    <div id="theCarousel" class="carousel slide" data-ride="carousel" data-interval="false">
        <ol class="carousel-indicators">
            <li data-target="#theCarousel" data-slide-to="0" class="active"></li>
            <li data-target="#theCarousel" data-slide-to="1"></li>
            <li data-target="#theCarousel" data-slide-to="2"></li>
        </ol>

        <div class="carousel-inner" role="listbox">
            <?php $loop = new WP_Query( array ( 'post_type' => 'carousel', 'orderby' => 'post_id', 'order' => 'ASC') ); ?>
            <?php while( $loop->have_posts() ): $loop->the_post(); ?>
                <div class="carousel-item" style="background-image: url('<?php the_field('carousel_image'); ?>')">
                    <div class="carousel-caption d-none d-md-block">
                        <h3><?php the_title(); ?></h3>
                        <p><?php the_field('carousel_description'); ?></p>
                    </div>
                </div>
            <?php endwhile; wp_reset_query();?>
        </div>

        <a class="carousel-control-prev" href="#theCarousel" role="button" data-slide="prev">
            <span class="carousel-control-prev-icon" aria-hidden="true"></span>
            <span class="sr-only">Previous</span>
        </a>
        <a class="carousel-control-next" href="#theCarousel" role="button" data-slide="next">
            <span class="carousel-control-next-icon" aria-hidden="true"></span>
            <span class="sr-only">Next</span>
        </a>
    </div>
</section>

【问题讨论】:

    标签: php wordpress twitter-bootstrap bootstrap-4 advanced-custom-fields


    【解决方案1】:

    你可以这样做

          <div class="carousel-inner" role="listbox">
                <?php 
                $iteration = 0;
                $loop = new WP_Query( array ( 'post_type' => 'carousel', 'orderby' => 'post_id', 'order' => 'ASC') ); ?>
                <?php while( $loop->have_posts() ): $loop->the_post(); $iteration++; ?>
                    <div class="carousel-item<?php if( $iteration == 1 ) echo ' active' ?>" style="background-image: url('<?php the_field('carousel_image'); ?>')" data-slide-to="<?php echo $iteration ?>">
                        <div class="carousel-caption d-none d-md-block">
                            <h3><?php the_title(); ?></h3>
                            <p><?php the_field('carousel_description'); ?></p>
                        </div>
                    </div>
                <?php endwhile; wp_reset_query();?>
            </div>
    
            <a class="carousel-control-prev" href="#theCarousel" role="button" data-slide="prev">
                <span class="carousel-control-prev-icon" aria-hidden="true"></span>
                <span class="sr-only">Previous</span>
            </a>
            <a class="carousel-control-next" href="#theCarousel" role="button" data-slide="next">
                <span class="carousel-control-next-icon" aria-hidden="true"></span>
                <span class="sr-only">Next</span>
            </a>
        </div>
    </section>
    

    【讨论】:

      【解决方案2】:

      这是我实现它的方式。 定义了两个变量。一个用于轮播指示器,一个用于轮播包装器并在循环中递增它们。

      $carousel_wrap = 0; $carousel_ind = 0;

      <section>
              <div id="theCarousel" class="carousel slide" data-ride="carousel" data-interval="false">
                  <ol class="carousel-indicators">
                      <?php $loop = new WP_Query( array ( 'post_type' => 'carousel', 'orderby' => 'post_id', 'order' => 'ASC') ); ?>
                      <?php while( $loop->have_posts() ): $loop->the_post(); $carousel_ind++; ?>
                          <li data-target="#theCarousel" data-slide-to="<?php echo $carousel_ind; ?>" class="<?php if( $carousel_ind == 1 ) echo 'active' ?>""></li>
                      <?php endwhile; wp_reset_query();?>
                  </ol>
      
                  <div class="carousel-inner" role="listbox">
                      <?php $loop = new WP_Query( array ( 'post_type' => 'carousel', 'orderby' => 'post_id', 'order' => 'ASC') ); ?>
                      <?php while( $loop->have_posts() ): $loop->the_post(); $carousel_wrap++; ?>
                          <div class="carousel-item <?php if( $carousel_wrap == 1 ) echo 'active' ?>" style="background-image: url('<?php the_field('carousel_image'); ?>')">
                              <div class="carousel-caption d-none d-md-block">
                                  <h3><?php the_title(); ?></h3>
                                  <p><?php the_field('carousel_description'); ?></p>
                              </div>
                          </div>
                      <?php endwhile; wp_reset_query();?>
                  </div>
      
                  <a class="carousel-control-prev" href="#theCarousel" role="button" data-slide="prev">
                      <span class="carousel-control-prev-icon" aria-hidden="true"></span>
                      <span class="sr-only">Previous</span>
                  </a>
                  <a class="carousel-control-next" href="#theCarousel" role="button" data-slide="next">
                      <span class="carousel-control-next-icon" aria-hidden="true"></span>
                      <span class="sr-only">Next</span>
                  </a>
              </div>
          </section>
      

      【讨论】:

        【解决方案3】:

        为此使用 for 循环。

        for ( $iteration = 0; $the_query->have_posts(); $iteration++  ) : $the_query->the_post(); 
        endfor;
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-05-08
          • 2013-12-06
          • 2015-12-30
          • 1970-01-01
          • 2020-08-11
          • 1970-01-01
          相关资源
          最近更新 更多