Home › Forums › General Issues › Show taxonomy image on custom post › Reply To: Show taxonomy image on custom post
The first thing that you need to do is to get the terms that the post is in.
$post_id = $post->ID;
$taxonomy = 'custom-category';
$args = array('fields' => 'ids');
$terms = get_post_terms($post_id, $taxonomy, $args);
$terms will hold an array term ids that the post is in. For more information on this see https://codex.wordpress.org/Function_Reference/wp_get_post_terms
Once you have a list of terms you can get the images from those terms. In the below I am using “image_field” for the name of your image field.
for (i=0; i<count($terms), i++) {
$term_post_id = $taxonomy.'_'.$terms[i];
$image_url = get_field('image_field', $term_post_id);
if ($image_url) {
// your code for showing the image here
}
}
For more about getting values in acf from things like taxonomy terms see the Get a value from other places section on this page: http://www.advancedcustomfields.com/resources/get_field/
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.