这是我的自定义帖子的代码。我不是 php 专家。所以请帮助我解决这个问题。
<?php $featuresitems=new WP_Query(array( 'post_type'=>'scbleftfeatures' )); ?>
<?php while( $featuresitems->have_posts()) : $featuresitems->the_post(); ?>
<div class="col-md-6 left-grid">
<div class="features-grid1">
<div class="feature">
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
</div>
<div class="icon5">
<?php the_post_thumbnail('features_icon_size'); ?>
</div>
<div class="clearfix"></div>
</div>
</div>
<div class="col-md-6 right-grid">
<div class="features-grid1">
<div class="feature">
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
</div>
<div class="icon5">
<?php the_post_thumbnail(); ?>
</div>
<div class="clearfix"></div>
</div>
</div>
<?php endwhile; ?>
最佳答案
问题有点含糊,但我假设您实际上想做的只是更改类 left-grid
和 right-grid
每第二个 div。在那种情况下,您不必重复整个 div,只需更改类即可。这可以在“计数器”($i
) 的帮助下完成。
<?php
$featuresitems = new WP_Query(array(
'post_type' => 'scbleftfeatures'
));
$i = 0;
?>
<?php
while( $featuresitems->have_posts()) : $featuresitems->the_post();
$i_is_even = ($i % 2 == 0);
?>
<div class="col-md-6 <?php if ($i_is_even) {echo 'left-grid'; } else { echo 'right-grid'; }; ?>">
<div class="features-grid1">
<div class="feature">
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
</div>
<div class="icon5">
<?php the_post_thumbnail('features_icon_size'); ?>
</div>
<div class="clearfix"></div>
</div>
</div>
<?php
$i ++;
endwhile;
?>
如果您对 %
运算符感到好奇,我建议您查看 http://php.net/manual/en/language.operators.arithmetic.php 上的模数运算符
如果您真的想在不同的 div 之间进行更改,您可以将整个 div 放在 if/else block 中,而不仅仅是类。
编辑: 不确定您为什么想要这个,因为您似乎正在使用 Bootstrap 网格。
关于php - 如何在不重复 wordpress 自定义帖子的情况下循环进入两个不同的 DIVS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31543263/