WordPress 默认会在循环(Loop)中显示置顶文章,但是在主题开发中,也许为了布局需求,你需要在 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();
//在这里插入循环内部代码
endwhile; //结束while
endif; //结束if
?> |
<?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();
//在这里插入循环内部代码
endwhile; //结束while
endif; //结束if
?>
‘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();
//在这里插入循环内部代码
endwhile; //结束while
endif; //结束if
?> |
<?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();
//在这里插入循环内部代码
endwhile; //结束while
endif; //结束if
?>
‘post__not_in’ => get_option( ‘sticky_posts’ ) 是关键参数,彻底排除置顶文章(凡是置顶文章都不输出)。假如你在已经在首页的其他地方(比如幻灯中)显示了置顶文章,那么,接下来的主循环中排除置顶文章,这样就可以避免重复显示。
更多示例,可以参考 WP_Query#Pagination_Parameters 一节。
来源:
https://www.wpdaxue.com/exclude-sticky-posts-from-the-loop.html
微信公众号
手机浏览(小程序)
Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages:
error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57
Warning: get_headers(): Failed to enable crypto in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57
Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_32251.jpg): failed to open stream: operation failed in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57