WordPress

在 WordPress 循环中排除置顶文章

取消置顶,按普通方式输出文章

<?php
$args = array(
	'posts_per_page' => 10, //每页显示10篇文章
	'ignore_sticky_posts' => 1 //取消文章置顶
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
 
//在这里插入循环内部代码
 
<?php endwhile; ?>
<?php endif; ?>

'ignore_sticky_posts' => 1 就是关键参数,取消文章置顶(即不在顶部显示),按照普通方式输出文章

 

彻底排除置顶文章,不输出

<?php
$the_query = new WP_Query( array( 'post__not_in' => get_option( 'sticky_posts' ) ) );
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
 
//在这里插入循环内部代码
 
<?php endwhile; ?>
<?php endif; ?>

'post__not_in' => get_option( 'sticky_posts' ) 是关键参数,彻底排除置顶文章(凡是置顶文章都不输出)。假如你在已经在首页的其他地方(比如幻灯中)显示了置顶文章,那么,接下来的主循环中排除置顶文章,这样就可以避免重复显示。

龙Ge

龙Ge

前端•不会,后端•是啥

喜欢在深夜写一点没用的代码。

评论

0

发表评论

邮箱不会被公开。
验证码