• Resolved hungzai

    (@hungzai)


    I have a hierarchy of categories of A < Bs < Cs where A is the grandparent, B is the parent and C is the child. There are many Bs and many Cs.

    There are no post in A. What I would like to do is to list B if the C under it has posts and list all the posts.

    So the result I would like to see is B with postings listed in C(which is under B)

    I found this code but somehow what it does is listing all the Bs including those with Cs that has no postings. I would like to only list those Bs that has postings under their Cs. Can anyone help me to troubleshoot the code? Thanks in advance.

    <?php $categories =  get_categories('hide_empty=0&parent=16');
    foreach ($categories as $category)
    {
            //Display the sub category information using $category values like
            echo '<h2>'.$category->name.'</h2>';
            echo '<ul>';
    
            foreach (get_posts('cat='.$category->term_id) as $post) {
                setup_postdata( $post );
                echo '<li><a href="'.get_permalink($post->ID).'">'.get_the_title().'</a></li>';
            }
            echo '</ul>';
        }
    ?>
Viewing 10 replies - 1 through 10 (of 10 total)
  • Hi hungzai,
    Try change your first line of code “<?php $categories = get_categories(‘hide_empty=0&parent=16’);”
    Use hide_empty=1
    This will work for you.
    Thanks

    Thread Starter hungzai

    (@hungzai)

    Thanks. I tried. It don’t work because this hide empty is referring to hiding the Bs but none of my posts are specifically had ticked any category on the Bs(parent) level but Cs. So when I set it at hide_empty=1, it returns nothing at all because technically no posts has ticked any categories on the parent level. All are ticked on the C level.

    Please Use this Code:

    $categories =  get_categories('hide_empty=0&parent=16’);
    
    foreach ($categories as $category)
    {
           $cat_ID = $category->cat_ID;
           $inrCategories =  get_categories("hide_empty=0&parent=$cat_ID");
    
           foreach ($inrCategories as $inrCategory)
           {
             if($inrCategory->count == 0)
             {
              continue;
             }
    
          //Display the sub category information using $category values like
             echo '<h2>'.$category->name.'</h2>';
             echo '<ul>';
    
             foreach (get_posts('cat='.$category->term_id) as $post) {
                 setup_postdata( $post );
                 echo '<li><a href="'.get_permalink($post->ID).'">'.get_the_title().'</a></li>';
             }
             echo '</ul>';
           }
    
    }

    Hope this will solve your purpose

    Thread Starter hungzai

    (@hungzai)

    Thanks. It did hide the empty categories on C level. However the code seems to have some bugs. Some categories are repeating itself. Some repeat itself 2 or 3 times and some as much as 8 times one after another.

    Another question I have is why does one category only produces 5 postings? Some of it has more than 20 postings. All have maximum 5 postings or lesser.

    Thank you so much for your time.

    Problem may be because multiple categories assigned to post.
    Please explain what you want once more , this time in more intuitive manner so that I get what you want.
    Once I get you completely, I assure you I can solve your problem.
    Please give a detailed explanation of your categories structure. Screenshots and links will help a lot.
    Thanks

    Thread Starter hungzai

    (@hungzai)

    My categories are structured in this manner.

    A > B1/B2/B3/B4 > C1/C2/C3/C4

    A is the Grandparent and B is the parent and there are many Bs. Every of those Bs can have multiple Cs and every of those Cs are unique(only assigned to one B). My posts are not listed in A level or B level but in the C levels. There are many Bs and some of them have zero postings in the C level.

    What I want to do is to list the categories in B level that has postings in the C level. For those Bs that has zero postings in C level, I would like to omit them.

    Your code just now seems to do the omitting part right. Only those Bs with postings in their C levels are listed. However there seems to have some issues with the way the counter is counting as it seems to repeat some of the Bs multiple times, one after another.

    Thanks so much.

    Thread Starter hungzai

    (@hungzai)

    Basiscally I want the code to do this

    Under Category A, check every B’s child for postings.

    If there are postings, echo title of B followed by title all the postings. Omit those Bs without any postings in their child categories.

    Hope this time I get what you want. You have explained your problem much better this time, thanks for that. It makes my work easy.

    Use this code and hopefully this time you marked your query as resolved ??

    $categories =  get_categories('hide_empty=0&parent=16’);
    
    foreach ($categories as $category)
    {
           $cat_ID = $category->cat_ID;
           $inrCategories =  get_categories("hide_empty=0&parent=$cat_ID");
    
           foreach ($inrCategories as $inrCategory)
           {
             if($inrCategory->count == 0)
             {
              continue;
             }
    
          //Display the sub category information using $category values like
             echo '<h2>'.$category->name.'</h2>';
             echo '<ul>';
    
             foreach (get_posts('cat='.$category->term_id) as $post) {
                 setup_postdata( $post );
                 echo '<li><a href="'.get_permalink($post->ID).'">'.get_the_title().'</a></li>';
             }
             echo '</ul>';
    
             break;
           }
    
    }

    Thanks

    Thread Starter hungzai

    (@hungzai)

    Perfect! You are genius! THANK YOU SO MUCH!

    Thread Starter hungzai

    (@hungzai)

    Hi, this code works perfectly. Now I would like to add the individual C level category of a particular post to the output. Echoing it just before the titles. Am struggling to do so. Could anyone help please?

    [ No bumping please. ]

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘List posts in sub-sub categories of a category.’ is closed to new replies.