How to Display Image on Taxonomy with Advanced Custom Fields (ACF)

By Posted in - Blog Post on July 28th, 2017 0 Comments

Sometimes things that should be easy are hard…

Today, I needed to display an Advanced Custom Fields (ACF) image on a custom taxonomy page.  Here’s the solution:

First, create the image field and apply it to the taxonomy like this:

Make sure you have the image field set to output the “Image Array” like this:

Now, go to your taxonomy term and add an image like this:

Finally, copy-and-paste code to your functions.php:

<?php
add_action( 'loop_end', 'output_before_taxonomy_loop' );
function output_before_taxonomy_loop(){
	if (is_tax()) {
 
		// Display the artist image
		$queried_object = get_queried_object();
		$taxonomy = $queried_object->taxonomy;
		$term_id = $queried_object->term_id;
		$terms = get_field( 'artist_image', $taxonomy.'_'.$term_id);
 
		if( $terms ) {
 
			echo '<img src="'. $terms['url'] .'" />';
 
		} else {
 
		    //do nothing
 
		}
 
	}
}
?>

Finally, baske in the glory that is Advanced Custom Fields’ taxonomy image fields!

Artist is https://cryns.com

Ain’t life grand? Well…mostly?

Comments are closed.