• Resolved hungzai

    (@hungzai)


    $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>';
           }
    
    }

    Basically this code check whether a post has any sub sub category under a sub category, if yes echo sub category and the title of the post.

    I would like to add the sub sub category to the front of the title. Am struggling to do so. Any help would be much appreciated. Thanks in advance.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter hungzai

    (@hungzai)

    Anyone have any idea? Basically just need to check the sub category under “$inrCategory” and echo it in front of <a href="'.get_permalink($post->ID).'">'.get_the_title().'</a>

    Thanks in advance.

    Thread Starter hungzai

    (@hungzai)

    Still struggling. Basically my structure is like this.

    A
    -B1
    –C1
    -B2
    –C2
    -B3
    –C3

    The posts are listed in Cs. So the above code checks for posts in C and will echo respective B with the titles of the posts. Now I would like to echo B and followed by C in front of the titles. Eg (C)titles.

    Struggling for 2 days. Would be grateful for any help. Thanks.

    Thread Starter hungzai

    (@hungzai)

    For those who are keen, the solution is here

    if (post_is_in_descendant_category($cat_ID))
    foreach((get_the_category()) as $childcat)if (cat_is_ancestor_of($cat_ID, $childcat))
    echo $childcat->cat_name . ' ';

    Just add this to after the line “setup_postdata( $post );”

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Checking for subsub category and echo it.’ is closed to new replies.