How to prevent empty category to list content of sub categories?

I have a category structure like following:

A
->B
  ->C
D

(B is sub category of A and C is sub category of B)

I have content only for category “C” and if I open Category “B” the content of category “C” is displayed. How can I prevent this? I want to display “No Posts” if I open Category “B”.

1 Answer
1

Assuming the context here is filtering out posts from category C when viewing the category B posts archive, you could accomplish this by using the query_posts keyword category__in (which does not include posts from child categories) instead of cat (which does include posts from child categories). Just add it in archive.php or category.php depending on what’s appropriate for you theme.

global $wp_query;
$args = array_merge( $wp_query->query, array(
    'category__in' => get_query_var('cat')
));
query_posts( $args );

Leave a Comment