gpt4 book ai didi

php - 用 div 在自定义 wordpress 循环中包装每 4 个帖子

转载 作者:可可西里 更新时间:2023-11-01 00:13:11 25 4
gpt4 key购买 nike

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

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

关于php - 用 div 在自定义 wordpress 循环中包装每 4 个帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36142796/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com