0

I've set up a hierarchical taxonomy. Each taxonomy has an image field I'm trying to output the title, image and link of child taxonomy on their parent page. I've managed to output the title and link but I'm stuck with getting the sub taxonomy image as it's a custom field.

I've used the code below.

<?php
  $term_id = get_queried_object()->term_id;
  $taxonomy_name = get_query_var( 'taxonomy' ); 
  $termchildren = get_term_children( $term_id, $taxonomy_name );
  $category_image = get_field('category_image');

  foreach ( $termchildren as $child ) {
      echo '<div class="col-sm-4 sub-caregory">';
          $term = get_term_by( 'id', $child, $taxonomy_name );
          echo '<a href="' . get_term_link( $child, $taxonomy_name ) . '">' . $term->name . '</a><div class="sub-caregory-image"><img src="' . $category_image['url'] . '" alt="' . $category_image['alt'] . '" /></div>';
      wp_reset_postdata();

      echo '</div>';
  }
?> 
2
  • What print_r( $category_image ); returns?
    – SLH
    Nov 1, 2014 at 22:48
  • It returns nothing.
    – Legin76
    Nov 1, 2014 at 23:04

1 Answer 1

0

You need to ask for the child term image field. You have only asked for the current page's query object image, which may be blank depending on what the query object is.

<?php
  $term_id = get_queried_object()->term_id;
  $taxonomy_name = get_query_var( 'taxonomy' ); 
  $termchildren = get_term_children( $term_id, $taxonomy_name );

  // Loop through each child taxonomy term
  foreach ( $termchildren as $child ) {
      // Get the term object
      $term = get_term_by( 'id', $child, $taxonomy_name );

      // Get the values we need to output
      $url = get_term_link( $child, $taxonomy_name );
      $name = $term->name;

      // We make the call here to get the image and we pass in the term we want the image for
      $image = get_field('category_image', $term );

      // Output it all
      echo '<div class="col-sm-4 sub-caregory">';
      echo '<a href="' . $url . '">' . $name . '</a><div class="sub-caregory-image"><img src="' . esc_url($image['url']) . '" alt="' . esc_attr($image['alt']) . '" /></div>';
      echo '</div>';
  }
?> 
0

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.