简体   繁体   中英

How to exclude categories from WordPress blog post page

I'm using a static page for my front page and another page for my blog post.

How can i exclude certain categories from showing up on my blog page. I've seen the code for the front page but can't get it to work for my blog page

function exclude_category( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'cat', '-4,-5,-6,-7,-8,-10,-12,-13' );
}
}
add_action( 'pre_get_posts', 'exclude_category' );
add_action( 'pre_get_posts', 'exclude_category_posts' );

function exclude_category_posts( $query ) {
if( $query->is_main_query() && $query->is_home() ) {
$query->set( 'cat', '-359, -2' );
    }
}

Try this by putting in your functions.php

It's strange your code should have worked try this way.

function exclude_category($query)
{
    if ($query->is_home() && $query->is_main_query())
    {
        //Alternative method
        $query->set('category__not_in', array(4, 5, 6, 7, 8, 10, 12, 13));
    }
}

add_action('pre_get_posts', 'exclude_category');

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM