使用div将自定义wordpress循环中的每4个帖子换行

时间:2016-03-21 22:26:14

标签: php wordpress loops row

    <?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();
   ?>

您好, 我以前从未这样做过。我试图在<div class="row"></div>

中的上面的循环中包含每4个帖子

1 个答案:

答案 0 :(得分:6)

这应该排除你

$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