How would I display all the sub category but one? I tried is_category('myslug') then return in the post loop but it wont work.

my code in the post loop


Stills return all the sub category even the one with slug myslug

  • Not entirely clear what you mean. From the codex on is_category: "This Conditional Tag checks if a Category archive page is being displayed". Are you trying to display a list of sub-category names or are you displaying an archive page of sub-categories? – Eckstein Feb 1 '14 at 19:13
  • I'm displaying a list of posts that are in a category and its sub categories, but there are posts from one category that I dont want to display – Jonathan de M. Feb 1 '14 at 23:05
  • Can you post some code from the page or template you're working on? It sounds like you'll need to alter the query for that page, but I just want to see what your code looks like now. – Eckstein Feb 2 '14 at 3:28

If you'd like to exclude certain subcategories in a category archive page, try put the following code in your functions.php:

function exclude_category( $query ) {
    if ( $query->is_category( 'myslug' ) && $query->is_main_query() ) {
        $query->set( 'cat', '-1,-1347' ); //Add your excluded subcategories ids
add_action( 'pre_get_posts', 'exclude_category' );

If it's not the case, we need your code to see the whole picture.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.