Retrieving ACF Fields from Taxonomy Terms in taxonomy.php
When building a custom taxonomy archive, you often need to display specific data attached directly to the term object rather than the post content. This is a common requirement for categorization systems where the "image" or description lives on the category itself. The Advanced Custom Fields (ACF) plugin handles this gracefully, but doing so dynamically within taxonomy.php requires understanding how WordPress resolves term IDs and handles field data retrieval.
The core challenge in your scenario is that you cannot hardcode the taxonomy name or the specific term ID because these vary depending on which archive page the user visits. You need a solution that automatically detects the current context. Fortunately, WordPress provides get_queried_object() to handle exactly this dynamic resolution.
Detecting the Current Term Context
Inside your taxonomy.php template, you can retrieve the currently queried object using get_queried_object(). This function returns a WP_Term object representing the term being displayed in the archive. From this object, you can extract both the taxonomy slug and the numeric term ID:
$queried_object = get_queried_object();
$taxonomy = $queried_object->taxonomy;
$term_id = $queried_object->term_id;
This approach ensures that regardless of whether the user is viewing category/news, tag/urgent, or any other taxonomy archive, $queried_object will always reflect the specific term currently in scope. This eliminates the need for global variables or complex conditional logic to detect the page state.
Retrieving Field Data: Object vs. String
Once you have the term data, you can pass it directly to get_field(). ACF is flexible enough to accept either a Term object or a formatted string representing that term.
The most robust method is passing the $queried_object directly to the function. This avoids any potential confusion between terms with identical names but different IDs:
$image = get_field('image', $queried_object);
If you prefer not to pass an object, ACF also supports a string format that concatenates the taxonomy name and term ID with an underscore separator. Using this method is strictly optional but functionally equivalent in most scenarios:
$image = get_field('image', $taxonomy . '_' . $term_id);
Handling the Output
It is important to note what get_field() returns when no image exists for that specific term. The function will return false rather than throwing an error. Before attempting to output the image, you should check if the value exists. If you try to echo or manipulate a false value directly in PHP without checking, it may result in blank output or unexpected behavior depending on how you intend to render it (e.g., using wp_get_attachment_image()).
A safe implementation pattern looks like this:
<div class="gradient-overlay">
<?php
$image = get_field('image', $queried_object);
if ($image) {
// Assuming the field stores an attachment ID or URL
if (is_numeric($image)) {
echo wp_get_attachment_image($image, 'medium');
} else {
echo '<img src="' . esc_url($image) . '" alt="Hero Image">';
}
}
?>
</div>
Why This Approach Matters
Using get_queried_object() is the standard pattern for term-specific logic in WordPress themes. It keeps your templates clean and decoupled from specific taxonomy names. If you later decide to add a new taxonomy like "Genres" or switch the field location, your taxonomy.php template remains unchanged because it relies on the current context rather than static values.
Additionally, passing the term object directly is generally preferred over string concatenation in modern ACF versions because it reduces the risk of typos in taxonomy names and handles special characters in taxonomy slugs automatically. However, both methods are supported by the plugin's documentation.