关闭边栏(ESC)打开边栏(ESC)
在文章显示时,排除置顶文章常用这二种方法:
‘ignore_sticky_posts’ => 1
‘post__not_in’ => get_option( ‘sticky_posts’ )
区别如下
取消置顶,按普通方式输出文章
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?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 就是关键参数,取消文章置顶(即不在顶部显示),按照普通方式输出文章。
如果不加这个的话,则置顶的文章会显示在前面。如侧边栏显示最新文章时,如果不加这个,那最新的文章就会显示在置顶文章下边了,排序上就显得不是按最新的时间排了。
彻底排除置顶文章,不输出
1 2 3 4 5 6 7 8 9 |
<?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’ ) 是关键参数,彻底排除置顶文章(凡是置顶文章都不输出)。假如你在已经在首页的其他地方(比如幻灯中)显示了置顶文章,那么,接下来的主循环中排除置顶文章,这样就可以避免重复显示。