将每4个帖子用div包装到自定义wordpress循环中

编程入门 行业动态 更新时间:2024-10-06 08:39:55
本文介绍了将每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循环中

本文发布于:2023-11-26 19:08:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1634825.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   帖子   div   wordpress

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!