dougang2749 2015-07-21 15:22
浏览 43
已采纳

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

this is the code of my custom post.I am not so expert in php.So Please help me in this matter.

<?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>
  • 写回答

1条回答 默认 最新

  • drvonr6573 2015-07-21 15:52
    关注

    Question is a little vague but I'm assuming what you actually want to do is just change the classes left-grid and right-grid for every second div. In that case you don't have to repeat the whole divs, just change the classes. This can done with help of a "counter" ($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;
    ?>
    

    If you're curious about the % operator I suggest you check out the Modulus operator on http://php.net/manual/en/language.operators.arithmetic.php

    And if you actually want to alter between different divs you can put the whole divs inside the if/else blocks instead of just the classes.


    Edit: Not sure why you'd want this as it seems you're using the bootstrap grid anyways.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?