置顶文章的查询
WordPress 文章列表文章置顶,使用 WP_query() 时, ignore_sticky_posts 参数设置 为 false,则会在查询结果的头部,附带所有的置顶文章。
但实际开发中,因为各种需求,最好的方式是使用两个 WP_query(),第一个 WP_query() 只列出置顶的文章,第二个 WP_query() 只列出不置顶的文章,然后剩下的 Ajax 请求,都不带置顶文章。
第一个查询:
$args = array(
'post__in' => get_option( 'sticky_posts' ),
'ignore_sticky_posts' => 1
);
$query = new WP_Query( $args );
第二个查询:
$args = array(
'ignore_sticky_posts' => true,
'post__not_in' => get_option( 'sticky_posts' )
);
$query = new WP_Query( $args );
判断文章是否置顶
if (is_sticky()) {
echo '已置顶';
}
if (is_sticky($post_id)) {
echo '已置顶';
}
声明:本站所有文章和图片,如无特殊说明,均为原创发布,转载请注明出处。