Display posts, subcategories and categories of custom post type and taxonomy

UPDATE:

I have found a solution, as per several threads on here and elsewhere. Please use this if this is useful to you.

It is the code of the taxonomy.php file in my Genesis theme.

It does the following:

  1. Displays all the Child category names and the articles related to those categories

EXAMPLE:

(breadcrumb) Health Topic A Page

  • Main Subcategory 1

    • Article 1
    • Article 2
  • Main Subcategory 2

    • Article 1
    • Article 2
  1. It displays JUST the articles belong to “Main Subcategory X”

EXAMPLE:

(breadcrumb) Health Topic A Page > Main Subcategory 1

  • Article 1
  • Article 2
function child_do_custom_loop() {

    //first get the current term

    $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); // get term
    $parent = get_term($term->parent, get_query_var('taxonomy') ); // get parent term
    $children = get_term_children($term->term_id, get_query_var('taxonomy')); // get children

    if(($parent->term_id=="") && (sizeof($children)>0)) { // no parent, has child - core main categories

        $args = array(
        'child_of' => $term->term_id,
        'taxonomy' => $term->taxonomy,
        'hide_empty' => 0,
        'hierarchical' => true,
        'orderby'=>'menu_order',
        'depth'  => 1,
        'title_li' => ''
        );


        $category = get_categories($args);

        foreach ($category as $subcat) {
            wp_reset_query();

            echo "<h3>" . $subcat->name . "</h3>";  //Get Parent Category Name

            $newargs = array(
             'post_type' => 'health-topics',
             'post_status' => 'publish',
                'tax_query' => array(
                     array(
                       'taxonomy' => $term->taxonomy,
                       'field' => 'slug',
                       'terms' => $subcat->slug,
                    ),
                ),
            );

            $posts = new WP_Query($newargs); 

                while($posts->have_posts()) : $posts->the_post(); ?>
                    <h5 id="post-<?php the_ID(); ?>" style="margin-left:20px;">
                    <a href="https://wordpress.stackexchange.com/questions/254844/<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
                    <?php the_title(); ?></a></h5><?
                endwhile;

        } // end foreach

    //echo "i have NO parent, but have children";
    }elseif(($parent->term_id!="" && sizeof($children)>0)) { // has parent and child - mid level cats, we dont have these yet

        //echo "i have a parent, and children";

    }elseif(($parent->term_id=="") && (sizeof($children)==0)){ ?>

        <h5>Articles</h5> <?

        if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

                <h5 id="post-<?php the_ID(); ?>">
                <a href="https://wordpress.stackexchange.com/questions/254844/<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
                <?php the_title(); ?></a></h5><?

        endwhile; 
        endif;


        //echo "i have NO parent and NO children";

    }elseif(($parent->term_id!="") && (sizeof($children)==0)) { // has parent, no child - last branch on tree

        ?><h3>Articles</h3> <?

        if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

                <h5 id="post-<?php the_ID(); ?>">
                <a href="https://wordpress.stackexchange.com/questions/254844/<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
                <?php the_title(); ?></a></h5> <?

        endwhile; 
        endif;

        //echo "i have a parent, but NO children";*/


    }
 }
/* END */