本文介绍了将每4个帖子用div包装到自定义wordpress循环中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<?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>
Hi, I've never done this before. I'm trying to wrap every 4 posts in the loop above inside a <div class="row"></div>
推荐答案这应该可以解决您的问题
This should sort you out
$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(); ?>改编自包装div在foreach循环PHP中大约每三个项目
更多推荐
将每4个帖子用div包装到自定义wordpress循环中
发布评论