0

I am trying to loop through the category taxonomy and display the posts attached to each term, with a heading (the term name) separating each term's posts (and also display all the terms, even those that are not attached to any posts).

Additionally, I need the child terms to be displayed as subheadings under their parent term's heading.

For example:

  • AUTOMOTIVE (top-level/parent term w/ no children)
    • Post 1
    • Post 2
  • BUILDING SUPPLY (parent term)
    • Drywall (child term)
      • Post 3
      • Post 4

After hours of reading and trial & error I have this code working to display just the top-level, parent terms first. I'm hoping a Wordpress ninja can make my day and guide me where to go now:

    <?php 
        $taxonomy = 'category'; // Choose the taxonomy
        // Get all the terms
        $terms = get_terms( $taxonomy, array(
            'hide_empty' => false,) // Show terms with no posts attached
            );
        foreach ($terms as $term) { // Cycle through terms, one at a time
            // Check and see if the term is a top-level parent - if so, display it
            $parent = $term->parent;
            if ( $parent=='0' ) {
                echo '<h2>' . $term->name . '</h2>';

                 ***Need to loop term posts here, with headings for child terms***

            } 
        }
    ?>

1 Answer 1

2

You need to first filter on parent category, then after if you got a child category of any parent category, then you need to find out post of that category.

Please try below code and let me know if you have any query:

<?php 
    $terms = get_terms( array(
        'taxonomy' => 'category', //Default category
        'hide_empty' => false,                  
    ) );

    echo "<ul>";
    foreach ($terms as $term) {                     
        $parent = $term->parent;
        if ( $parent == '0' ) {
            /* Parent category */
            echo '<li>' . $term->name . '</li>';

            /* Child category */
            $childrens = get_categories( array ('parent' => $term->term_id ));
            echo "<ul>";
            foreach($childrens as $children) :                          
                $args = array(
                    'post_type' => 'post',
                    'tax_query' => array(
                        array(
                            'taxonomy' => 'category', //Default category
                            'field'    => 'slug',
                            'terms'    => $children->slug,
                        ),
                    ),
                );
                echo '<li>' . $children->name . '</li>';    
                /*  Child category posts */
                ?>                          
                <ul>
                <? 
                $loop1 = new wp_Query($args);
                while($loop1->have_posts()) : $loop1->the_post();
                    the_title( '<h6>', '</h6>' );
                endwhile;
                wp_reset_query(); 
                ?>
                </ul>
                <?php   
            endforeach;
            echo "</ul>";

            /*  Parent category posts */
            if (empty($childrens))
            {
            ?>                          
            <ul>
            <? 
            $args = array(
                'post_type' => 'post',
                'tax_query' => array(
                    array(
                        'taxonomy' => 'category',
                        'field'    => 'slug',
                        'terms'    => $term->slug,
                    ),
                ),
            );
            $loop = new wp_Query($args);
            while($loop->have_posts()) : $loop->the_post();
            the_title( '<h6>', '</h6>' );
            endwhile;
            wp_reset_query(); 
            ?>
            </ul>
            <?php
            }   
        }
    }
    echo "</ul>";
?>
1
  • Thanks for the help. I would not have figured out all of that code :( I am getting some extra <ul> and </ul> though in the output. Could you help with this?: <ul><li>Adhesives</li><ul></ul><ul></ul><li>Automotive</li><ul></ul><ul><h6>Post 1</h6></ul><li>Building Supply</li><ul><li>Drywall</li> <ul><h6>Post 2</h6></ul></ul></ul>
    – codeview
    Mar 20, 2017 at 17:01

Your Answer

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

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