0

I would like to list out the child terms related to the taxonomy archive and then list posts belonging to the child terms below the term name.

I currently have the following code which successfully lists out the child terms.

$this_term = get_queried_object();

$args = array(
'parent' => $this_term->term_id,
'orderby' => 'slug',
'hide_empty' => false
 );

$child_terms = get_terms( $this_term->taxonomy, $args );
echo '<ul>';

foreach ($child_terms as $term) {

//List the child topics 
echo '<li><h3><a href="' . get_term_link( $term->name, $this_term->taxonomy ) . '">' . $term->name . '</h3></a></li>'; 

// Try to list the contained posts (DOES NOT WORK)
?> <div><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a><div><?php

 }//end foreach
  echo '</ul>';

However, I need it to then display the permalinks for articles that belong to that child term. My current method of using 'the_permalink' just returns the same post in each topic.

Can anyone point me in the right direction?

  • You will need to create a custom loop for each child term. – Bruno Cantuaria Mar 21 '17 at 14:01
1

I was able to get this working with the following code. I used the relation operator AND, referenced the var ($this_term) that contained get_queried_object(); to set the taxonomy in the tax_query and adjusted the term field in the tax_query.

 $this_term = get_queried_object();
 $args = array(
'parent' => $this_term->term_id,
'orderby' => 'slug',
'hide_empty' => false
 );
 $child_terms = get_terms( $this_term->taxonomy, $args );
 echo '<ul>';
 foreach ($child_terms as $term) {

 // List the child topic
 echo '<li><h3><a href="' . get_term_link( $term->name, $this_term->taxonomy ) . '">' . $term->name . '</a></h3>'; 

 // Get posts from that child topic  
$query = new WP_Query( array(
  'post_type' => 'kb',
  'tax_query' => array(
    'relation' => 'AND',
    array(
      'taxonomy' => $this_term->taxonomy,
      'field'    => 'slug',
      'terms'    => array( $term->slug )
    )
  )
) );

 // List the posts
 if($query->have_posts()) {
      while($query->have_posts()) : $query->the_post(); ?>
           <li><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li><?php
      endwhile;
 } else  { echo "no posts";}


 // close our <li>
 echo '</li>';
 } //end foreach
0

You need another query that will find the posts associated with each child term, the post-containing <div> needs to be changed to an <li> since it is still inside the <ul>. You might want to indent the posts underneath the child term link. This should point you in the general direction:

foreach ($child_terms as $term) {

     // List the child topic
     echo '<li><h3><a href="' . get_term_link( $term->name, $this_term->taxonomy ) . '">' . $term->name . '</a></h3>'; 

     // Get posts from that child topic
     $args = array(
          'post_type' => 'yourposttype',
          'tax_query' => array(
                   'taxonomy' => $term->term_id,
                   'field' => 'term_id',
                   'terms' => 'terms'
          )
     );
     $query = new WP_Query( $args ); 
     // List the posts
     if($query->have_posts()):
          while($query->have_posts()) : $query->the_post(); ?>
               <li><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li><?php
          ehdwhile;
     endif;

     // close our <li>
     echo '</li>';
 } //end foreach

I am not as familiar with taxonomy queries, so the tax_query portion may need to be adjusted and I welcome edits. The key is you need a query to grab the posts you need, and once you have a Loop you can then use the_permalink etc. as desired.

  • Thanks for your help! I was able to find a solution as listed above. – tmgale12 Mar 23 '17 at 9:09

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.