Just like the title, I'm trying to get an image from a custom field I attached to a taxonomy. I currently have a taxonomy called city and the term would be some like Albuquerque, NM. I have created an image custom field ('city_hero_image') with ACF's and selected the ID as the return value. Looking at the source, in the src tag it says 'unknown' and when doing a print_r it returns basically an empty object array.
My taxonomy-city.php is
$context['posts'] = Timber::get_posts();
$context['categories'] = Timber::get_terms('city');
$cover_image_id = get_field('city_hero_image');
$context['cover_image'] = new TimberImage($cover_image_id);
Timber::render( $templates, $context );
In my taxonomy-city.twig I have
<img src="{{cover_image.get_url}}" class="img-responsive" alt="">
php $categories = get_categories( array( 'orderby' => 'name', 'order' => 'ASC' ) ); foreach( $categories as $category ) { if($category->name != "Uncategorized") { $cat_title = get_term_meta( $category->term_id, '_pagetitle', true ); echo ' <div class="col-md-4"><a href="' . get_category_link($category->term_id) .
To retrieve a field value as a variable, use the get_field() function. This is the most versatile function which will always return a value for any type of field. To display a field, use the the_field() in a similar fashion. Here's a basic usage example, and please be sure to view the code example page for more.
When you run get_field
, the function will try to guess the object where it should look for the custom field from the context. The context is normally The Loop.
So, if you run $cover_image_id = get_field( 'city_hero_image' );
, ACF will try get the custom field city_hero_image
from the current post’s ID, which it can’t find. There’s also no city_hero_image
defined on a post, it’s defined on your term.
get_field
inside the loop, it will take the current post’s ID to load a field. But, with Timber you’re never inside The Loop. Timber is in fact used to get rid of The Loop.city_hero_image
), you’d need to explicitly tell get_field
to look in that taxonomy with get_field( 'city_hero_image', 'city_termid' )
, while termid will be the id of the term you want the data for.In Timber, get_field
works a little different. Timber will populate the post or term object with the custom fields as properties.
If you have a custom field city_hero_image
in your taxonomy, you can access it in multiple ways:
in PHP
$category->city_hero_image
get_field
method of a Timber\Term object:$category->get_field('city_hero_image)
in Twig
{{ category.city_hero_image }}
gef_field
method:{{ category.get_field('city_hero_image') }}
The same works for ACF fields on Timber\Post objects. You only need to call get_field
explicitly when you want to access data of field types Repeater or Flexible Content.
Let’s look at this in your example. You first need to get the term that is displayed. For archive pages, you can use the function get_queried_object()
, which will return an object depending on the archive that is accessed:
WP_Term
object.WP_Post_Type
object.WP_User
object.You have a term archive (taxonomy-city.php
), so it would be
$term = new \Timber\Term( get_queried_object() );
$cover_image = $term->city_hero_image;
$context['term'] = $term;
$context['cover_image'] = new \Timber\Image($cover_image);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With