我如何循环到两个不同的DIVS而不重复wordpress自定义帖子


How can I loop into two different DIVS without repeating in wordpress custom post

这是我的自定义帖子的代码。我对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; ?>

问题有点模糊,但我假设你实际上想要做的是每隔一个div更改left-gridright-grid的类,在这种情况下,你不必重复整个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块中而不是仅仅放在类中


编辑:不知道为什么你想要这个,因为它似乎你正在使用引导网格。