I have a custom taxonomy setup - Downloads. Within downloads I have several subcategories. I'd like to list the titles of those entries, with the entry title being a link to an Advanced Custom Field (text type). I have something that loops through and prints the subcategory names, but I'm stuck on how to display entry information within each subcategory. Here's what I have so far:

    if ( have_posts() ) : ?>

            <header class="page-header">
            </header><!-- .page-header -->
            <div class="row">
                <div class="col-sm-12">


             * Include the Post-Format-specific template for the content.
             * If you want to override this in a child theme, then include a file
             * called content-___.php (where ___ is the Post Format name) and that will be used instead.

                $term = get_queried_object();
                $term_id = $term->term_id;
                $taxonomy_name = $term->taxonomy;

                $termchildren = get_term_children( $term_id, $taxonomy_name );

                echo '<ul>';
                foreach ( $termchildren as $child ) {
                    $term = get_term_by( 'id', $child, $taxonomy_name );
                    echo '<li><a href="' . get_term_link( $term, $taxonomy_name ) . '">' . $term->name . '</a></li>';
                echo '</ul>';   

                wp_link_pages( array(
                    'before' => '<div class="page-links">' . esc_html__( 'Pages:', 'fernox' ),
                    'after'  => '</div>',
                ) );


    else :

        get_template_part( 'template-parts/content', 'none' );

    endif; ?>


I think I just need a loop within the current 'foreach' but I'm not sure how to go about it.


If I understood your problem correctly within the $termchildren loop, you'll need to add a get_posts() tax_query that will find all posts that have the current loops term. I think somthing similar to the following will work:

if ( have_posts() ) :

    echo "<h2>Subcategories</h2>";

    // if you're on a single post, it will return the post object
    // if you're on a page, it will return the page object
    // if you're on an archive page, it will return the post type object
    // if you're on a category archive, it will return the category object
    // if you're on an author archive, it will return the author object
    $term = get_queried_object();

    echo "Current \$term is: <pre>";
    echo "</pre>";

    $termchildren = get_term_children( $term->term_id, $term->taxonomy );

    echo '<ul>';
    foreach ( $termchildren as $child ) {
        $childTerm = get_term_by('id', $child, $term->taxonomy );
        echo '<li><a href="' . get_term_link( $childTerm, $term->taxonomy ) . '">' . $childTerm->name . '</a>';

        // posts with this term         
        $childTermPosts = get_posts(array(
          'post_type' => 'post', // WTV your post_type is for Downloads tax
          'numberposts' => -1,
          'tax_query' => array(
              'taxonomy' => $term->name,
              'field' => 'id',
              'terms' => $child,
              'include_children' => true

        // then loops the posts
        if (count($childTermPosts) > 0) {
            echo "<ul>"

            foreach ($childTermPosts as $childTermPost)
                echo "<li><a href='".get_permalink( $childTermPost->ID )."'>{$childTermPost->post_title}</a></li>";

            echo "</ul>"

        echo "</li>";

    } //end foreach

    echo '</ul>';   

else :

    echo "get_template_part(template-parts/content) here";
    //get_template_part( 'template-parts/content', 'none' );


I've noted in comments that get_queried_object() returns a lot depending on your current template file.

The above is not tested, but hopefully it gives an idea of what can be done. I commented and removed things because sometimes to narrow down your problem or even what you're after is to minimize the code, and having the code spit-out current variables - adding all the html and pagination etc last.

  • Great, thank you! I got it working after I changed "'taxonomy' => $term->name" to "'taxonomy' => $taxonomy_name" in the $childTermPosts array. Is this safe and correct? I'm guessing so since it's working but it would be good to be sure. Also Added required curly braces in the inner foreach loop. – user2265915 Dec 12 '17 at 15:26
  • glad you were able to resolve the bug I accidentally had! – David Sword Dec 12 '17 at 17:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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