-1

I am trying to show respective child taxonomy term names and images on their parent term pages.

For example:

Parent Term A Child Term 1a Child Term 2a Child Term 3a On the Parent Term A taxonomy page, I want to show the taxonomy name and image for the 3 child terms 1a, 2a, and 3a and have each linked to their respective taxonomy term page. Child term 1a (name and image), for example would link to the Child term 1a taxonomy page.

Right now i can show the child nae and link to parent page. but wants to show the child name with image. it would be great if you could help me with that. following is my code to display child in parent page:

$term_id = get_queried_object_id();
$taxonomy_name = 'post_tag';
$term_children = get_term_children( $term_id, $taxonomy_name );

foreach ( $term_children as $child ) {
           $term = get_term_by( 'id', $child, $taxonomy_name );
           $string .= '<li><a href="' . get_term_link( $child, $taxonomy_name ) . '">' . $term->name . '</a></li>';
   }
   return $string;

Sorry I forget to mention earlier I am using 'Taxonomy Images' to add image for tags. I have updated code where it display child(s) with name and image in parent page. But the problem is it display all child(s) of all parents. But I want to display specific child(s) for specific parent. Below is my code:

$terms = apply_filters( 'taxonomy-images-get-terms', '',
    array(
        'taxonomy' => 'post_tag',
    ) );
foreach ((array)$terms as $term) {
    $parent = $term->parent;
    if ( $parent!='0') {
        $string .= '<span class="tagbox"><a class="taglink" href="' . esc_url( get_term_link( $term, $term->taxonomy ) ) . '"><span class="tagname">'.wp_get_attachment_image( $term->image_id, 'detail' ).'<p>'.$term->name.'</p></span></a><span class="tagcount">'. $term->count .'</span></span>';
    }
}
return $string;

It would really appreciable if you could help me to guide how I could filter the parent ID to display only that parent child details (name and image). Thanks.

  • What are you using to add the image to your taxonomy terms? That's not Core functionality, so it will depend on how you are adding the images. – WebElaine Feb 5 '18 at 20:25
  • i am using Taxonomy Images plugin – user9310297 Feb 6 '18 at 3:43
  • You should refer to its documentation for how to use it. – Jacob Peattie Feb 6 '18 at 8:38
  • Thanks for your reply. I did go through the documentation but couldn't find any suitable solution... – user9310297 Feb 6 '18 at 14:22
1

You can try this.

$parent_terms = get_terms( array(
    'taxonomy'   => 'post_tag',
    'hide_empty' => false,
) );

foreach ( $parent_terms as $terms) {
      $term = get_terms(array(
        'taxonomy'   => 'post_tag',
        'hide_empty' => false,
        'parent'     => $terms->term_id
      ) );
      foreach ($term as $term_child){
         $string .= '<li><a href="' . get_term_link( $term_child->term_id ) . 
                     '">' . $term_child->name . '</a></li>';
      }
}

return $string;
  • hi, thank you so much...i tried your code but it gives same child for all parents. actually i want to display specific child(s) for specific parent with child name and image. – user9310297 Feb 6 '18 at 3:37
  • i come to following code where child is display with name and image. but problem is it display all child(s) in parent page regardless the parent id. – user9310297 Feb 6 '18 at 3:47
  • $terms = apply_filters( 'taxonomy-images-get-terms', '', array( 'taxonomy' => 'post_tag', ) ); foreach ((array)$terms as $term) { $parent = $term->parent; if ( $parent!='0') { $string .= '<span class="tagbox"><a class="taglink" href="' . esc_url( get_term_link( $term, $term->taxonomy ) ) . '"><span class="tagname">'.wp_get_attachment_image( $term->image_id, 'detail' ).'<p>'.$term->name.'</p></span></a><span class="tagcount">'. $term->count .'</span></span>'; } } return $string; – user9310297 Feb 6 '18 at 3:47

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.