最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

categories - Sticky posts not working when a specific category is set

programmeradmin0浏览0评论

I would like to show sticky posts at the top of an archive list for a specific category on the top page as well as on archive pages. I've been Googling for a working solution and found a few clues similar to this:

if(!empty(get_option('sticky_posts'))){
    $args = array(
        'posts_per_page' => -1,
        'posts__in'      => get_option('sticky_posts'),
        'category_name'  => 'EVENTS'
    );

    $catquery = new WP_Query($args);
} 

The above code only works if 'category_name' => 'EVENTS' is commented out, so it seems as if you cannot show sticky posts at the top of the archive list as long as a category is called. Although some wrote something like 'WordPress can only show sticky posts on the front page.', but this does not seem to be the case making me terribly confused.

Someone please help me work this out.

Thank you in advance.

I would like to show sticky posts at the top of an archive list for a specific category on the top page as well as on archive pages. I've been Googling for a working solution and found a few clues similar to this:

if(!empty(get_option('sticky_posts'))){
    $args = array(
        'posts_per_page' => -1,
        'posts__in'      => get_option('sticky_posts'),
        'category_name'  => 'EVENTS'
    );

    $catquery = new WP_Query($args);
} 

The above code only works if 'category_name' => 'EVENTS' is commented out, so it seems as if you cannot show sticky posts at the top of the archive list as long as a category is called. Although some wrote something like 'WordPress can only show sticky posts on the front page.', but this does not seem to be the case making me terribly confused.

Someone please help me work this out.

Thank you in advance.

Share Improve this question edited Apr 2, 2019 at 5:31 phatskat 3,1741 gold badge18 silver badges26 bronze badges asked Apr 2, 2019 at 2:40 FizzlerFizzler 757 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

As per documentation for WP_Query(), category_name should be category slug. So replace EVENTS with the slug of category EVENTS

'category_name'  => 'events'  // Assuming that 'events' is slug of category `EVENTS`

OR

'cat'  => 5'   // Replace 5 with id of  of category `EVENTS`

I hope this helps.

发布评论

评论列表(0)

  1. 暂无评论