0

I have a custom post type 'downloads' which i have a have a custom template set up to get terms of to begin with. Im now trying to set up my taxonomy template so that it displays sub-categories if there are any, but if not to show posts for that term.

Below is what my taxonomy template includes so far and this works fine to the point that it displays the child categories, but it doesnt then show the posts for that category at the end. Id like it so that if there are child categories then show them, else show the posts for that category.

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

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

                        <?php foreach ( $termchildren as $child ) {
                        $term = get_term_by( 'id', $child, $taxonomy_name ); ?>
                            <li>
                              <?php echo $term->name; ?>
                              <?php echo $term->description; ?>
                            </li>
<?php } ?>
0

Please try this

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

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

  <?php foreach ( $termchildren as $child ) {
    $term = get_term_by( 'id', $child, $taxonomy_name ); ?>
    <li>
      <?php echo $term->name; ?>
        <?php echo $term->description; ?>
    </li>
    <?php }
if ( have_posts() ) :

    /* Start the Loop */
    while ( have_posts() ) : the_post();

        echo get_the_title($post->ID);


    endwhile;

        the_posts_pagination( array(
        'prev_text' => twentyseventeen_get_svg( array( 'icon' => 'arrow-left' ) ) . '<span class="screen-reader-text">' . __( 'Previous page', 'twentyseventeen' ) . '</span>',
        'next_text' => '<span class="screen-reader-text">' . __( 'Next page', 'twentyseventeen' ) . '</span>' . twentyseventeen_get_svg( array( 'icon' => 'arrow-right' ) ),
        'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'twentyseventeen' ) . ' </span>',
        ) );
?>

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.