0

I have a list of subcategories in a sidebar (children of top level category) and I also get a list of posts underneath each category. I only want to show those posts when we are under that subcategory or when we are viewing one of the posts. Each post comes under top level category and subcategory and I don't know how to get current subcategory id/name. I understand you get a category ID like this: $category = get_the_category(); $catID = $category[0]->cat_ID; but what about when a post belongs to subcategory and category and I just need a subcategory id I believe? Current code:

<?php  $cats = get_categories('child_of=6'); 
$this_category = get_category($cat);

//echo $parent_category;
   foreach ($cats as $cat) :
    $args = array(
    'category__in' => array($cat->term_id)
     );
    $my_query = new WP_Query($args); 
        if ($my_query->have_posts()) : ?>

        <li><a href="<?php echo get_category_link($cat); ?>"><?php echo $cat->name; ?></a>


        <?php if ($this_category->category_parent != 0) { ?>

        <ul class="children">
         <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>     
        <?php /*general loop output; for instance: */ ?>
       <li>- <a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>

        <?php endwhile; ?>
        </ul>
        <?php } ?>
        </li>

        <?php else : 
        echo 'No Posts for '.$cat->name;                
        endif; 

2 Answers 2

2

If you use the follow code:

<?php  $cats = get_categories('child_of=6'); 

        foreach ($cats as $cat) :
            $this_category = get_category($cat);
            $args = array(
                'category__in' => array($cat->term_id)
            );
            $my_query = new WP_Query($args); 
            if ($my_query->have_posts()) : ?>

            <li><a href="<?php echo get_category_link($cat); ?>"><?php echo $cat->name; ?></a>

            <?php if ($this_category->category_parent != 0) { ?>

                <ul class="children">
                <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>     
                <?php /*general loop output; for instance: */ ?>
                <li>- <a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>

                <?php endwhile; ?>
            </ul>
            <?php } ?>
            </li>

            <?php else : 
                echo 'No Posts for '.$cat->name;                
            endif; 
       endforeach;     
?>

you will get something like:

subcategory 1

* - Post title 1
* - Post title 2

subcategory 2

* - Post title 3

As you can see per sub category will appears only the Posts in that sub category. Note I move $this_category = get_category($cat); inside the foreach.

2
  • Thanks for your answer leticia. I moved $this_category = get_category($cat); inside the foreach loop, but it doesn't look like it's making any difference. I believe that I am moving a bit closer to the solution though. What I thought of now is: When we go through loop and show subcategories, to show posts under current subcategory we could check if slug is matching with permalink and then show children, but then I also need to show those posts when we are under single post view. I wonder what's the simplest solution?
    – Petra
    Commented Feb 10, 2012 at 10:03
  • Hello Petra. What I recommend is that you test the exact code I paste because in my test page is working fine, showing per sub category the link to the respective Posts. After you get that piece of code working you can change to fulfil any other requirement. About "need to show those posts when we are under single post view", I'm not sure what you mean. Normally in your single post view template the Post is showed.
    – leticia
    Commented Feb 10, 2012 at 18:47
0

You can filter for the subcategory by checking what the category parent is as so: $category_object->category_parent. The parent of any top level category will be set to 0, and the parents of the subcategories will not.

5
  • I have updated the code above: I have now added if ($this_category->category_parent != 0) { ?> before loop and What this does: On parent category page it's doing what it should: it shows only the subcategories of this category without posts. But on subcategory page it shows all the subcategories with all the posts. I understand that's because we are in a loop, but still don't know how to resolve this.
    – Petra
    Commented Feb 9, 2012 at 12:47
  • What about using something like if( $this_category->category_parent ) != 0 || $this_category->parent != $cat->parent ), that way you're checking if the parent is 0 or if the parent is the page you're on.
    – mor7ifer
    Commented Feb 9, 2012 at 13:25
  • Ok I tried your suggestion, but it doesn't work. Now, I always get lists of posts - under parent category and under any subcategory.
    – Petra
    Commented Feb 9, 2012 at 14:17
  • Sorry, second one should be ==, not !=. These are things you should be troubleshooting on your own, by the way.
    – mor7ifer
    Commented Feb 9, 2012 at 14:22
  • Thanks a lot for your help, but I still can't get it to work.
    – Petra
    Commented Feb 9, 2012 at 14:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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