0

I'm using the following code to generate a list of only the child terms within my taxonomy. What I'd also like to do is display a list of the child terms for each parent category on a taxonomy-mytax-myterm.php template for each parent. I'm trying to manipulate the code below to do this (while excluding the parent still) but I'm not sure how / where to specify the current parent and to grab all the terms under the parent.

Also, curious if it would be possible to achieve pagination using this method on a taxonomy archive like this? I have found ways of achieving pagination via page-template, but learned it would not work on archive-template for CPT. Not sure if this applies to taxonomy archive templates as well?

<?php
$args = array(
  'orderby' => 'name',
  );
$terms = get_terms( 'mytaxname', $args );
 if ( !empty( $terms ) && !is_wp_error( $terms ) ){
    foreach ( $terms as $term ) {

        if( 0 == $term->parent )
            continue;

        echo '<a href="' . get_category_link( $term->term_id ) . '">' . $term->name . '</a><br/>';
    } 
}
?>

1 Answer 1

1

When you are on a taxonomy page, you can get the parent from the term being displayed by using the following code with get_queried_object. See get_terms for the objects that are returned

$queried_object = get_queried_object('term');
  $term = $queried_object->parent;

To get the taxonomy, you can simply just add

$tax = $queried_object->taxonomy;

below the code above. This can then be used in the code above as follows

<?php

$queried_object = get_queried_object('term');
  $term_parent = $queried_object->parent;
  $tax = $queried_object->taxonomy;

$args = array(
  'parent' => $term_parent,
  'orderby' => 'name',
  );

$terms = get_terms( $tax, $args );
 if ( !empty( $terms ) && !is_wp_error( $terms ) ){
    foreach ( $terms as $term ) {

        echo '<a href="' . get_category_link( $term ) . '">' . $term->name . '</a><br/>';
    } 
}
?>

The code above can be used either above or below the loop in your taxonomy-mytax-myterm.php to display a list of terms from the term's parent.

As for your second question, the answer is no. Archive pages are not meant to create a paginated index list of objects. They are exclusively meant to display post in that specific hierarchy, so a taxonomy archive is meant to display a paginated "list", if you will, of posts from a specific term, not a paginated index list of terms.

To understand what is classified as archives to need to dig into core. Take a look at wp-includes/query.php#L1615

1615     if ( $this->is_post_type_archive || $this->is_date || $this->is_author || $this->is_category || $this->is_tag || $this->is_tax )
1616     $this->is_archive = true;

So from that, the following is regarded as archive pages

  • category.php

  • tag.php

  • archive.php

  • author.php

  • taxonomy.php

  • date.php

So, if you need to have a paginated index page of terms, you are going to need to stick to a page template for that purpose

For additional info, go and check the following links

2
  • Thank you very much Pieter, that was very beneficial to me! Most gracious for your time on this.
    – streetfire
    Commented Aug 25, 2014 at 22:03
  • It is my pleasure. Glad you found this useful. Enjoy :-) Commented Aug 26, 2014 at 4:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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