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
<?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 (a WP_Term object) 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. Replace prod_ctg_banner with 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 the full size image, and [0] is its URL.
  • The img tag prints the banner URL through esc_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.
Share on X (opens in a new tab) Share on LinkedIn (opens in a new tab) Share on Facebook (opens in a new tab)

ChatGPT (opens in a new tab) Claude (opens in a new tab) Gemini (opens in a new tab) Perplexity (opens in a new tab) Grok (opens in a new tab)

More Insights

See More