In a previous post, I showed an example of how to retrieve taxonomy images that are attached via Advanced Custom Fields.
However, what if you want to access it outside of the category.php
, tag.php
or taxonomy.php
templates?
Field Info
In this example, your ACF field is named ‘associated_image’ and ‘chosen-image-size’ is the desired image size that is set within your theme. Note: the ACF field is also set to return an array.
Full Snippet Example:
// Retrieves post categories' object outside the loop.
$cat = get_the_category();
// Narrows down to get term ID.
$term_id = $cat[0]->term_id;
// Get all term data via term ID.
$term = get_term( $term_id );
$attachment_id = get_field( 'associated_image', $term );
// URL of image at specific size.
$media = $attachment_id['sizes'][ $size ];
return $media;
Additional Reading:
- ACF Docs: Taxonomy
- ACF Docs: How To Get Values from Another Post