php - 如何在不重复 wordpress 自定义帖子的情况下循环进入两个不同的 DIVS

标签 php css wordpress

这是我的自定义帖子的代码。我不是 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-gridright-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/

相关文章:

php - 将 SVG 转换为 PNG/JPEG/BMP 和反之亦然?

php - WebSockets - 通过 php 发送 json 数据

javascript - 为什么这在 JS 而不是 PHP 中有效?

css - 使用 CSS 删除重复出现的点

javascript - 我在 Wordpress 中加载更多帖子 Ajax 按钮不起作用

php - 从 WooCommerce 中的当前用户订单中获取订单 ID

php - 当用户编辑他们的个人资料时,如何避免图像被覆盖为空白?

javascript - 在 div 中裁剪和居中任意大小的图像

CSS 菜单在 IE 中不起作用

css - 如何忽略图像的透明像素?