【问题标题】:Wrap every 4 posts in a custom wordpress loop with a div使用 div 将每 4 个帖子包装在自定义 wordpress 循环中
【发布时间】:2016-03-21 22:26:14
【问题描述】:
    <?php
      $args = array(
      'post_type' => 'college',
      'posts_per_page' => -1,
      'order' => 'DESC',
      'orderby' => 'menu_order'
      );

      $the_query = new WP_Query( $args );
      if ( $the_query->have_posts() ) :
      while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

      <div class="col-3">
        <?php the_title(); ?>
      </div>

   <?php
   endwhile;
   endif;
   wp_reset_postdata();
   ?>

嗨, 我以前从来没有这样做过。我正在尝试将上面循环中的每 4 个帖子包装在 &lt;div class="row"&gt;&lt;/div&gt;

【问题讨论】:

    标签: php wordpress loops row


    【解决方案1】:

    这应该可以解决您的问题

    $args = array(
        'post_type' => 'college',
        'posts_per_page' => -1,
        'order' => 'DESC',
        'orderby' => 'menu_order'
    );
    
    $the_query = new WP_Query($args);
    if ($the_query->have_posts()) :
        $counter = 0;
        while ($the_query->have_posts()) : $the_query->the_post();
            if ($counter % 4 == 0) :
                echo $counter > 0 ? "</div>" : ""; // close div if it's not the first
                echo "<div class='row'>";
            endif;
            ?>
            <div class="col-3">
                <?php the_title(); ?>
            </div>
            <?php
            $counter++;
    
        endwhile;
    endif;
    wp_reset_postdata();
    ?>
    

    改编自Wrapping a div around every third item in a foreach loop PHP

    【讨论】:

    • 太棒了 :) 这是一个很好的挑战,让我写了一些东西
    猜你喜欢
    • 2015-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-30
    • 2014-01-22
    • 2013-03-03
    • 1970-01-01
    相关资源
    最近更新 更多