Home › Forums › General Issues › Show Taxonomy Image on Posts › Reply To: Show Taxonomy Image on Posts
So I worked out out to show the Taxonomy image in a single or archive template of a post that belongs to the category..
$image = get_field('jg_category_tag', 'category_5');
if ( in_category( 'my-category' ) ) {
echo '<img src="' . $image['url'] . '" /> ';
}
So i use a conditional check to see if the post belongs to the category and if it does it displays the category image.
But now what is the more optimal way of writing the code when I have numerous categories with images and to only display when a post belongs to them.
The code below would go into the loop but is not efficient…
// vars
$image = get_field('jg_category_tag', 'category_5');
$image7 = get_field('jg_category_tag', 'category_7');
$image8 = get_field('jg_category_tag', 'category_8');
if ( in_category( 'my-category' ) ) {
echo '<img src="' . $image['url'] . '" /> ';
}
if ( in_category( 'your-category' ) ) {
echo '<img src="' . $image7['url'] . '" /> ';
}
if ( in_category( 'another-category' ) ) {
echo '<img src="' . $image8['url'] . '" /> ';
}
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.