0

Issue: I am unable to display ACF custom fields while looping through taxonomies.

I have an ACF text field called 'icon'. After applying it to my custom taxonomy 'topic' (and categories while troubleshooting) it displays as intended on the taxonomy archive page. However when I list the taxonomy on any other page I cannot access the ACF field, just the standard fields.

As this is used globally on several pages, I currently have this in my functions:

$context['topicTax'] = get_terms('topic');

And in the .twig template:

{% for item in topicTax %}
   <li>
       <p>{{ item.meta('icon') }} {{ item.name }}</p>
   </li>
{% endfor %}

I have also tried

$context['topicTax'] = Timber::get_terms('topic');
$context['topicTax'] = $topicTax;

but this doesn't display anything.

I found this post to have a similar issue (though not using Timber) but have not managed to replicate the solution.


Though I have been using twig for several years, I am still fairly new to Wordpress, and have skipped straight to Timber. Apologies if I am overlooking something simple.

1 Answer 1

0

In your PHP file...

$context['topicTax'] = Timber::get_terms('topic');
Timber::render('my-template.twig', $context);

... is the correct structure. The problem is that WP's get_terms returns "dumb" objects that don't have methods for retrieving ACF info. If you still run into issues, verify that those objects are Timber\Terms rather than WP_Term

Also, if you're looking for the more active forum: StackOverflow is where I normally manage/do support

3
  • Thank you for the reply, in the future I will post on StackOverflow. This is something I tried, though after taking your advice I have a different issue. This line of code, or rather adding Timber:: in front of get_terms('topic'); renders the page blank, or not at all. I have tried renaming the variable to topicTaxonomy to make sure there is no conflict elsewhere, and I have tried isolating this to a single page rather than globally in my functions file. Dec 1, 2017 at 7:58
  • Scratch that, while testing I had left behind some broken code. This worked, thank you Jared! Dec 1, 2017 at 8:04
  • You bet, glad it worked!
    – Jared
    Dec 4, 2017 at 2:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.