代码之家  ›  专栏  ›  技术社区  ›  Cray

从WordPress中的特定类别获取粘性文章

  •  0
  • Cray  · 技术社区  · 6 年前

    我想检查一个类别是否有粘性的帖子。 为此,我使用 $sticky = get_option( 'sticky_posts' ); archive.php .

    但它显示了博客上所有的贴纸。不仅来自显示的类别。 我是否可以添加任何内容以仅显示显示显示的类别或标签中的粘性文章?

    如果有粘帖,我需要的是对/错。如果有粘帖,我需要一个带有ID的数组。

    1 回复  |  直到 6 年前
        1
  •  1
  •   dineshkashera    6 年前

    我希望下面的代码可以帮助您:

        $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();
    

    有关更多帮助,请参阅此链接 click here