【发布时间】:2016-07-19 17:56:18
【问题描述】:
我需要 PHP 代码在 WordPress 循环中的 DIV 中添加每 2 个项目。
例如,我需要这样:
<div class="wrap">
post
post
</div>
<div class="wrap">
post
post
</div>
<div class="wrap">
post
post
</div>
这是我的 wordpress 循环,但不工作,我需要在 DIV 中每 2 个帖子:
<?php if ( have_posts() ) : // If have post start. ?>
<?php $i = 0; ?>
<?php while ( have_posts() ) : the_post(); // Start Loop: ?>
<?php if ( $i % 2 == 0) : ?>
<div class="wrap">
<?php endif; ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php the_content(); ?>
</article>
<?php if ( $i % 2 == 0 ) : ?>
</div>
<?php endif; ?>
<?php $i++; endwhile; // End Loop. ?>
<?php endif; // If have post end. ?>
谢谢。
【问题讨论】:
-
如果您在主循环中,那么您可以使用以下内容进行检查:
if( $wp_query->current_post % 2 == 0 ){...}或者您可以在循环外设置计数器$i=0;(if ( have_posts() ) : while ( have_posts() ) : the_post();),然后检查它是否为模数2 为 0:$if( i % 2 == 0)并在每次迭代时递增 ($i++;),在endwhile之前。 -
不工作@dingo_d
-
显示无效的代码。
-
@dingo_d 阅读我的 wordpress 循环代码。
-
这个link可以帮助你。
标签: php wordpress loops for-loop while-loop