![](/img/trans.png)
[英]Display list of subcategories of a category page in a post - wordpress
[英]How to display 13 post with specific category recent post in middle in each page of WordPress blog list page
我正在尝试在博客列表页面中实现这种布局,我实现了一点点,但是无法在每个页面中显示特定类别的最新帖子,我该如何实现,这是我的代码
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 13,
);
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$loop = new WP_Query( $args );
$count = 0;
while ( $loop->have_posts() ) : $loop->the_post(); $count++ ?>
<?php if( $count % 7 === 0 ){ ?>
<div class="col-md-12">
<?php the_title(); ?>
</div>
<?php } else { ?>
<div class="col-md-4">
<?php the_title(); ?>
</div>
<?php } ?>
<?php endwhile; ?>
<div class="d-flex justify-content-center">
<nav class="pagination ">
<?php pagination_bar( $loop ); ?>
</nav>
</div>
<?php wp_reset_postdata();
?>
如果你不想在你的循环中有重复的帖子,首先你需要从主查询中排除“精选”类别,你可以为此使用pre_get_posts操作。 使用它来设置帖子的主要查询数量( 'posts_per_page' = 12
),或者从/wp-admin/options-reading.php
然后在你的主循环中每隔 7nth 个帖子,对你的“特色”类别进行自定义查询,每页仅 1 个帖子。
<?php
$count = 0;
while ( have_posts() ) : the_post(); $count++;
if(7 === $count) :
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$featured = get_posts(
array(
'category' => $featured_category_id,
'posts_per_page' => 1,
'paged' => $paged,
)
);
setup_postdata($featured);
// Display the featured post
wp_reset_postdata();
else :
// Display regular post;
endif;
endwhile;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.