从 WordPress 中的特定类别获取粘性帖子

Get sticky posts from specific category in WordPress

我想检查某个类别是否有置顶帖子。 为此,我在 archive.php.

上使用 $sticky = get_option( 'sticky_posts' );

但是它显示了博客中的所有置顶帖子。不仅来自显示的类别。 有什么我可以添加以仅显示显示类别或标签中的置顶帖子吗?

如果有置顶帖,我需要的是true/false。如果有置顶帖子,我需要一个包含 ID 的数组。

希望以下代码对您有所帮助:

    $paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
    $sticky = get_option( 'sticky_posts' );
    $args = array(
        'cat' => 3,
        'ignore_sticky_posts' => 0,
        'post__in' => $sticky,
        'paged' => $paged
    );
    $sticky_posts = new WP_Query( $args );
    if ( $sticky_posts->have_posts() ) : while ( $sticky_posts->have_posts() ) : 
       $sticky_posts->the_post() );
    //Loop markup here
   endwhile; endif;
  //IMPORTANT
wp_reset_postdata();

如需更多帮助,请参阅此 link click here