【问题标题】:Wordpress post loop with incremental value具有增量值的 Wordpress 后循环
【发布时间】:2013-09-07 15:36:02
【问题描述】:

我有一个循环,为每个循环的帖子显示一个列表项(用于滑块),如下所示:

<?php while (have_posts()) : the_post(); ?>
<li data-target="#myCarousel" data-slide-to="0"<?php if( $wp_query->current_post == 0 && !is_paged() ) { ?> class="active"<?php } else { ?><?php } ?>></li>
<?php endwhile; ?>

但是,对于它生成的每个列表项,我需要将 data-slide-to 的值增加 1。例如,如果循环有 3 个帖子,最终结果如下所示:

<li data-target="#myCarousel" data-slide-to="0" class="active"></li>
<li data-target="#myCarousel" data-slide-to="1"></li>
<li data-target="#myCarousel" data-slide-to="2"></li>

如何逐步增加 data-slide-to 值?

【问题讨论】:

    标签: php wordpress wordpress-theming


    【解决方案1】:

    在while循环中添加一个计数器:

    <?php 
      //We want to start with 0 so $counter will be -1
      $counter = -1;
      while (have_posts()) : the_post(); $counter++ 
    ?>
    
    <li data-target="#myCarousel" data-slide-to="<?php echo $counter; ?>"<?php if( $wp_query->current_post == 0 && !is_paged() ) { ?> class="active"<?php } else { ?><?php } ?>></li>
    
    <?php endwhile; ?>
    

    【讨论】:

    • 工作但必须将 $counter = 0 更改为 $counter = -1 因为我需要它以 0 而不是 1 开头。谢谢!
    • 不客气,@jessback,我将更新答案以供参考。
    • 如果我有 100 个帖子,每页有 10 个帖子,解决方案是什么?
    猜你喜欢
    • 2015-06-17
    • 2016-08-10
    • 1970-01-01
    • 2011-11-07
    • 1970-01-01
    • 1970-01-01
    • 2023-02-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多