Archive Page

How to Display Child Taxonomies Dynamically on Parent Taxonomy Archive Pages in WordPress

If you use categories, tags, or custom taxonomies on your WordPress site, you might want to display their child terms on the parent taxonomy archive page. For example, if you have a category called Technology with child categories like Mobile, AI, and Web Development, showing these child categories on the Technology archive page makes navigation much easier for your visitors.

In this guide, we’ll show you how to dynamically display child taxonomies on any taxonomy archive page in WordPress without hardcoding the taxonomy name.

Why Display Child Taxonomies?

Displaying child taxonomies on parent taxonomy archives offers several benefits:

  • Improves user navigation – Visitors can quickly drill down into more specific topics.
  • Enhances site structure – Shows the hierarchy and relationship between your content categories.
  • Reduces clicks – Helps users find the content they want faster.
  • Works across multiple taxonomies – Useful for default taxonomies like categories and tags, as well as custom ones.

How It Works

The idea is simple:

  1. Detect when a user is viewing a taxonomy archive page.
  2. Get the current term and find its taxonomy type automatically.
  3. Retrieve all child terms of that parent taxonomy.
  4. Display the child terms before the posts loop.

This method is dynamic, so it works for categories, tags, and any custom taxonomy without changing the code manually.

Step-by-Step Implementation

You can add the following code to your theme’s functions.php file or a site-specific plugin.

add_action( 'loop_start', 'wps_child_terms_before_posts' );
function wps_child_terms_before_posts( $query ) {
    if ( ! is_main_query() || !( is_tax() || is_category() || is_tag() ) ) {
        return;
    }
    $term = get_queried_object();
    if ( ! $term || is_wp_error( $term ) ) {
        return;
    }
    $taxonomy = $term->taxonomy;
    $term_id  = $term->term_id;
    $children = get_terms( array(
        'taxonomy'   => $taxonomy,
        'parent'     => $term_id,
        'hide_empty' => true,
    ) );
    if ( ! empty( $children ) && ! is_wp_error( $children ) ) {
        echo '<div class="child-taxonomy-list-wrapper">';
        echo '<h3>Sub-' . esc_html( ucfirst( $taxonomy ) ) . '</h3>'; 
        echo '<ul class="child-taxonomy-list">';
        foreach ( $children as $child ) {
            $link = get_term_link( $child );
            if ( is_wp_error( $link ) ) {
                continue;
            }
            echo '<li><a href="' . esc_url( $link ) . '">' . esc_html( $child->name ) . '</a></li>';
        }
        echo '</ul>';
        echo '</div>';
    }
}

If you don’t want to put the snippet in functions.php (via hooks), you can drop the logic directly inside archive.php (or taxonomy.php, or even category.php if your theme has those).

That way, the code runs inline in the template file instead of being globally hooked.

Here’s the version you can paste into your archive.php above the loop:

if ( is_tax() || is_category() || is_tag() ) {
    $term = get_queried_object();
    if ( $term && ! is_wp_error( $term ) ) {
        $taxonomy = $term->taxonomy;
        $term_id  = $term->term_id;
        $children = get_terms( array(
            'taxonomy'   => $taxonomy,
            'parent'     => $term_id,
            'hide_empty' => true,
        ) );
        if ( ! empty( $children ) && ! is_wp_error( $children ) ) {
            echo '<div class="child-taxonomy-list-wrapper">';
            echo '<h3>Sub-' . esc_html( ucfirst( $taxonomy ) ) . '</h3>';
            echo '<ul class="child-taxonomy-list">';
            foreach ( $children as $child ) {
                $link = get_term_link( $child );
                if ( is_wp_error( $link ) ) {
                    continue;
                }
                echo '<li><a href="' . esc_url( $link ) . '">' . esc_html( $child->name ) . '</a></li>';
            }
            echo '</ul>';
            echo '</div>';
        }
    }
}

Where to put it

  • In your archive.php file, look for the main loop (something like if ( have_posts() ) : while ( have_posts() ) : the_post();).
  • Place this block of code just above the loop, so the child taxonomy list shows before the posts.

Here is how the child taxonomy list will look to your visitors:

Child taxonomy list

Code Explanation

  • is_main_query() – Ensures the code runs only for the main query, not for side queries like widgets.
  • is_tax() || is_category() || is_tag() – Checks that you’re on a taxonomy archive page (categories, tags, or custom taxonomies).
  • get_queried_object() – Fetches the current term being viewed, including its taxonomy and ID.
  • get_terms() with parent => $term_id – Retrieves only the child terms of the current taxonomy term.
  • hide_empty – Controls whether to display terms that have no posts assigned.

Customizing the Output

You can adjust the snippet to match your site’s needs:

Style the list
The code outputs a <div> and <ul> with classes. You can style them in your theme’s CSS:

.child-taxonomy-list-wrapper { margin: 20px 0; padding: 10px; background: #f9f9f9; border: 1px solid #ddd; border-radius: 6px; } .child-taxonomy-list li { list-style: none; margin: 5px 0; } .child-taxonomy-list a { text-decoration: none; color: #0073aa; } .child-taxonomy-list a:hover { text-decoration: underline; }

Testing the Feature

Once added, visit different taxonomy archives on your site:

  • Category archives – Child categories should appear before the posts.
  • Tag archives – If tags have hierarchy, child tags will display.
  • Custom taxonomy archives – Any hierarchical taxonomy will show its child terms.

If a term has no children, nothing will appear, keeping the page layout clean.

Conclusion

By dynamically displaying child taxonomies on parent taxonomy archive pages, you make your WordPress site easier to explore and more structured. The code above works for categories, tags, and custom taxonomies without needing manual edits for each case.

With a little CSS, you can also style the child taxonomy list so it integrates seamlessly with your theme design. This small improvement can make a big difference in navigation and user experience on your site.

Leave a Reply

Your email address will not be published. Required fields are marked *