TransWikia.com

WordPress - List Sub Categories and Sub-Sub Categories, With Posts

WordPress Development Asked by pee2pee on December 5, 2020

I have the following code. It allows me to show sub categories and documents within them. The thing is, a sub category may contain further sub categories. The code below will show documents belonging to a sub-sub (grand child) category at sub (parent) level.

Please can anyone shed light on how to solve this so document only show underneath the category they are directly under? In addition, highlighting what is a sub-sub (grandchild) category as well? All posts titles should be linked to the post

<?php
$categories =  get_categories('child_of=34');  
foreach  ($categories as $category) {
    //Display the sub category information using $category values like $category->cat_name
    echo '<h3>'.$category->name.'</h3>';
    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>';
}
?>

So something as follows

  • Sub Cat 1
    • Post 1
    • Post 2
  • Sub Cat 2
    • Sub Sub Cat 1
      • Post 3
    • Sub Sub Cat 2
      • Post 4

One Answer

Try This :

<!-- Category Archive Start -->
<ul class="catArchive">
<?php
$catQuery = $wpdb->get_results("SELECT * FROM $wpdb->terms AS wterms INNER JOIN $wpdb->term_taxonomy AS wtaxonomy ON ( wterms.term_id = wtaxonomy.term_id ) WHERE wtaxonomy.taxonomy = 'category' AND wtaxonomy.parent = 0 AND wtaxonomy.count > 0");

$catCounter = 0;

foreach ($catQuery as $category) {

    $catCounter++;

    $catStyle = '';
    if (is_int($catCounter / 2)) $catStyle = ' class="catAlt"';

    $catLink = get_category_link($category->term_id);

    echo '<li'.$catStyle.'><h3><a href="'.$catLink.'" title="'.$category->name.'">'.$category->name.'</a></h3>';
        echo '<ul style="margin-left:15px;">';

        query_posts('category__in='.$category->term_id.'&showposts=5');?>

        <?php while (have_posts()) : the_post(); ?>
            <li><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
        <?php endwhile; ?>

            <li><a href="<?php echo $catLink; ?>" title="<?php echo $category->name; ?>">More <strong><?php echo $category->name; ?></strong></a></li>
     <li> <?php
     $sub_cat_id = $category->term_id;
     $get_sub_args = array('child_of' =>$sub_cat_id);
             $categories_arr =  get_categories($get_sub_args);  
             //print_r ($categories_arr);
    foreach  ($categories_arr as $sacategory) {
            //Display the sub category information using $category values like $category->cat_name
            echo '<h2>'.$sacategory->name.'</h2>';
            echo '<ul style="margin-left:15px;">';

            foreach (get_posts('cat='.$sacategory->term_id) as $post) {
                setup_postdata( $post );
                echo '<li><a href="'.get_permalink($post->ID).'">'.get_the_title().'</a></li>';   
            }  
            echo '</ul>';
        }

     ?></li>



        </ul>
    </li>
    <?php } ?>
</ul>
<!-- Category Archive End -->
    </div>

Answered by vrajesh on December 5, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP