簡體   English   中英

使用 div 將每 4 個帖子包裝在自定義 wordpress 循環中

[英]Wrap every 4 posts in a custom wordpress loop with a div

    <?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 個帖子包裝在<div class="row"></div>

這應該把你整理出來

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

改編自foreach循環PHP中的每個第三項周圍的div包裝

您可以按照我的代碼進行操作,@chapskev 的帖子很好,但是在結束后缺少一個關閉的 div

<?php $counter = 0; // 4  per list ?>
<?php foreach ($arr as $key => $value) {
  if ($counter % 4 == 0) :
    echo $counter > 0 ? "</div>" : ""; // close div if it's not the first
    echo "<div class='group'>";
  endif;
  echo 
  <<<TEXT
  <span>content</span>
  TEXT;
  $counter++;
  }
  echo "</div>";
?> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM