Show ACF Custom Fields Of A Taxonomy Term

Quick Summary
- Pass the term as the second argument of get_field() to read ACF values saved on a category or other taxonomy term.
- The example gets the current term with get_queried_object() and shows its banner image, falling back to a default image when none is set.
- Set the image field's Return Format to Image ID, and remove the stray semicolon after .jpg in the fallback URL.
When you add ACF fields to a taxonomy, for example a banner image for each category, the values are saved on the term, not on a post. To read them on the term’s archive page, you pass the term to get_field() as its second argument. This example shows the banner image of the current term and falls back to a default image when no banner is set.
Add the code to the archive template that displays the term, in your child theme.
<?php
$i_url = get_template_directory_uri();
$i_url .= "/assets/img/banner-dummy.jpg;";
?>
<?php
$term = get_queried_object();
$p_image = get_field('prod_ctg_banner', $term);
$normal_p_img = wp_get_attachment_image_src($p_image, "full");
?>
<img src="<?php if (!empty($p_image)):?>
<?php echo esc_url($normal_p_img[0]); ?>
<?php else: ?>
<?php echo $i_url; ?>
<?php endif; ?>"
alt="">How it works
- The first block builds the fallback URL: the theme folder URL from
get_template_directory_uri()plus the path of a default banner image, joined with.=as in appending strings in PHP. get_queried_object()returns the term being viewed (aWP_Termobject) on a category, tag or custom taxonomy archive.get_field( 'prod_ctg_banner', $term )reads the field from that term. ACF accepts the term object directly; a string such as'term_12', where 12 is the term ID, works too. Replaceprod_ctg_bannerwith your own field name.- Set the image field’s Return Format to Image ID, because the value is passed to
wp_get_attachment_image_src(). That function returns an array for thefullsize image, and[0]is its URL. - The
imgtag prints the banner URL throughesc_url()when a banner is set, and the fallback URL otherwise.
The same second argument works for any field type on a term. For a text field, get_field( 'your_field', $term ) returns the text, which you should escape before printing.
Things to fix before using it
- The fallback string has a semicolon inside the quotes, right after
.jpg, so the default URL points to a file that doesn’t exist. Remove that semicolon. - The fallback URL is printed without escaping; wrapping it in
esc_url()as well keeps the output consistent.
Related articles
- a grid of category images from ACF term fields: loop over every term instead of the current one
- WooCommerce category thumbnails without ACF: product categories already have a built-in image field
