2

I need to show a list of custom taxanomy terms assigned to the current post type with a name and a custom field. (Created with Advanced Custom Fields)

Currently I use the following code, to show a list of the custom taxanomy terms assigned to the current post:

echo '<ul class="taxanomy-name">';
echo get_the_term_list( $post->ID, 'taxanomy-name', '<li>', ',</li><li>', '</li>' );
echo '</ul>';

Allthough I want to show a custom field from each taxanomy term along with the name. The code for showing the custom field on each taxanomy term page template is:

the_field('custom-field-name', 'taxanomy-name_ID');

My question is, how do i show the custom field, along with the custom taxanomy term name on the list?

Example:

<ul>
 <li>
  Taxanomy Term Name 1
  <img src="custom-taxanomy-term-1-custom-field-image">
 </li>

 <li>
  Taxanomy Term Name 2
  <img src="custom-taxanomy-term-2-custom-field-image">
 </li>
</ul>

I hope you can help me.. It's been bugging me for hours. Thanks!

1

Use the get_the_terms function instead so you can build the list manually and access the term_id for each term.

$terms = get_the_terms( $post->ID, 'taxanomy-name' );
echo '<ul>';
foreach( $terms as $term ):
    echo '<li>';
    echo $term->name;
    the_field( 'custom-field-name', 'taxanomy-name_' . $term->term_id );
    echo '</li>';
endforeach;
echo '</ul>';

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.