Im creating a document library with a custom post type of ‘downloads’ and custom taxonomy for categories of ‘downloadCategory’.
Below is what i have so far which works fine and then i have a taxonomy template set up to handle the individual download view.

However this doesnt seem to handle multiple levels of categories, as i have created a child category and its still appearing in my main loop below.

I ideally want it so that it shows the top level categories first, then any child categories or posts when you click into it.

How do i go about setting this up so it handles multiple levels?

<?php
    $tax = 'downloadCategory';

    $terms = get_terms( $tax, [
      'hide_empty' => true,

    ]);

    foreach( $terms as $term ) {

      if(  $term->count > 0 ) {  ?>

        <li>
          <div class="category">
            <h4><a href="<?php echo get_term_link( $term );?>"><?php echo $term->name; ?></a></h4>
            <p><?php echo $term->description; ?></p>
            <a class="btn btn-outline btn-primary" href="<?php echo get_term_link( $term );?>">View</a>
          </div>
        </li>

  <?php }
} ?>

Read more here: Creating multilevel wp taxonomy query


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: