I have the following category structure:

Category x >> Category x Sub >> Category x Sub Sub 

In my index.php file, I have created links to category x and so on.

So when the user clicks on the link in the main page, they go this page: wordpress/category/categoryx/

Only posts published under category x sub should be displayed here, the problem is that in this page, posts published under category x sub sub are also displayed.

In my archive.php I used :

  <?php while ( have_posts() ) : the_post(); if (in_category($cat)) { ?>
  <?php } endwhile; ?>

this correctly hides the categoy x sub sub posts in the main category but it also hides them from their own sub category.

So in this page: wordpress/category/categoryx/category-x-sub/

the posts published under category x sub sub are hidden too.

/* exclude sons */
$_categories = get_categories(array( 'child_of' => 16));
$exclude = '';
foreach($_categories as $category){
    $_exclude .= '-'.$category->term_id.',';
$_exclude = substr($_exclude,0,-1);
$_cats = $cat.','.$_exclude;
query_posts(array( 'cat' => $_cats ));
while ( have_posts() ) : the_post(); 
|improve this answer|||||
  • 1
    Never ever use query_posts, ever. There have been quite a lot written about just how bad it is. Also, Do not dump code as answers. Your code is confusing. Please file an edit and explain what it does and how it works – Pieter Goosen Oct 30 '15 at 12:32

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.