加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – WordPress后循环增量值

发布时间:2020-12-13 21:49:08 所属栏目:PHP教程 来源:网络整理
导读:我有一个循环显示每个循环的列表项(对于滑块),如下所示: ?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?ph
我有一个循环显示每个循环的列表项(对于滑块),如下所示:

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

如何逐步增加数据 – 幻灯片 – 值?

解决方法

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读