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

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:
acf image field on

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

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

Finally, copy-and-paste code to your functions.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, bask in the glory that is Advanced Custom Fields’ taxonomy image fields!

acf taxonomy image display
Artist is https://cryns.com

Ain’t life grand? Well…mostly?

Posted in

Toby Cryns

Toby Cryns is a freelance CTO, expert WordPress developer, and teacher.

He offers free advice to improve your freelance biz.

He also publishes small droppings every now and them to https://twitter.com/tobycryns and https://twitter.com/themightymo.com

Follow Toby's contributions on Github and WP.org.