1、is_sticky() 判断文章是否置顶
2、get_option(‘sticky_posts’): 获取置顶文章ID,包含所有置顶文章ID的数组
1.
(图片来自网络侵删)上面便是在query_post中调用文章的方法,详细阐明一下
‘post__in’ => get_option(‘sticky_posts’), //在置顶文章中调取文章‘posts_per_page’ => 5, //获取五篇置顶文章‘ignore_sticky_posts’ => 1 //默认值为0,不用除置顶文章
若是想打消置顶文章外的别的文章用 ‘post__not_in’ => get_option(‘sticky_posts’), 这样就可以在调用列表时打消置顶文章
用WP_Query调用置顶文章和上面的方法有点类似
<?php
$args = array(
'posts_per_page' => -1,
'post__in' => get_option( 'sticky_posts' )
);
$sticky_posts = new WP_Query( $args );
while ( $sticky_posts->have_posts() ) : $sticky_posts->the_post();?>
<li>
<a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
</li>
<?php endwhile; wp_reset_query();?>
如果只显示置顶文章那么用is_sticky()判断即可。
<?php
$args = array(
'posts_per_page' => -1,
'post__in' => get_option( 'sticky_posts' )
);
$sticky_posts = new WP_Query( $args );
while ( $sticky_posts->have_posts() ) : $sticky_posts->the_post();
if(is_sticky()){
?>
<li>
<a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
</li>
<?php } endwhile; wp_reset_query();?>