Display child taxonomy until the last child

Got some ideas for your requirement. Check below code…

$parent=get_queried_object()->term_id;
$child = get_categories('child_of=".$parent. "&hide_empty=0&echo=0&taxonomy=custom_taxonomy');
    if(count($child)>0){
    $i=0;
     foreach ( $child as $row ) { $i++;
        echo '<li><a href="'.get_term_link($row,$row->taxonomy).'">'.$row->name.'</a></li>';
        if(count($child)==$i){
            $args=array( 'post_type' => 'custom_post','order' => 'DESC', 'posts_per_page'=>1,'tax_query'  => array( array('taxonomy' => 'custom_taxonomy','terms' =>$row->term_id, 'field' => 'id' )) );
            $second_query = new WP_Query( $args );
                if ($second_query->have_posts()) :
                    while ($second_query->have_posts()) : $second_query->the_post();
                        echo get_the_title();
                    endwhile; 
                endif; wp_reset_query();
        }
    }
}

Leave a Comment