Okay… so another question if you don’t mind. I’m additionally hoping to display an image assigned to this taxonomy term. The image field was also created by ACF (industry_image) in a separate field group and attached to my taxonomy term “industries.”
// grab the "related industries" terms from the current queried object
$related = get_field('related_industries', $queried_object);
// loop through the returned terms and display the name and image
if ($related) {
foreach ($related as $term) {
echo $term->name;
// what I want to display in addition
echo $term->[ACF field assigned to this term, 'industry_image']
}
}
My $related variable above is only including the related_industries field and not the other other custom field, industry_image. Perhaps I need a special query to grab all the custom fields for the current taxonomy term.
Thanks John… worked like a charm. I feel like an idiot, but learning! Much appreciated.
UPDATE: Same settings as above but updated code. Now getting proper list of custom field id’s on my related term, but can’t seem to display the name instead of the ID.
$queried_object = get_queried_object();
$taxonomy = $queried_object->taxonomy;
$term_id = $queried_object->term_id;
// load thumbnail and related industries for this taxonomy term
$thumbnail = get_field('industry_image', $queried_object);
$related = get_field('related_industries', $queried_object);
print '<img src="';
print $thumbnail;
print '" class="industry-page-img" />';
echo '<ul>';
foreach ( $related as $term ) {
echo '<li>' . $term . '</li>';
}
echo '</ul>';
The above returns the image as expected, but only displays the proper term ID. I assumed printing the $term->name would display the field name, but it doesn’t.
My real-world application needed repeater fields, so here are repeater fields from a category page:
$current_category = single_cat_title("", false);
$term_id = get_queried_object_id($current_category);
if(get_field('state_links', 'category_' . $term_id))
{
echo '<h2><span>';
echo single_cat_title() . ' FFA Links';
echo '</span></h2>';
echo '<ul id="double" class="state">';
while(has_sub_field('state_links', 'category_' . $term_id))
{
echo '<li class="state"><a href="' . get_sub_field('state_link', 'category_' . $term_id) . '" target="_blank">' . get_sub_field('state_link_title', 'category_' . $term_id) . '</a></li>';
}
echo '</ul>';
}
Ah, that did it! THANK YOU!
Here was my final code that actually got the current category ID for me:
$current_category = single_cat_title("", false);
$term_id = get_queried_object_id($current_category);
the_field('test_field', 'category_' . $term_id);
I’m sure there’s a more efficient way.
Thank you again!
Thanks for your quick reply. There’s probably something I’m not understanding about $term_id. I have a regular WP post category of “stories-by-state” which has subcategories of each state (alabama, alaska, etc.). I don’t think the fact that I’m retrieving subcategory info has any effect on the outcome.
Given your instruction, I would have something like the folowing:
the_field('test_field', 'alabama_' . $term_id);
or
the_field('test_field', 'alabama_16');
where 16 is the the number I get from the ur:l /wp-admin/edit-tags.php?action=edit&taxonomy=category&tag_ID=16&post_type=post
Neither worked and I”m not 100% sure how to pull $term_id.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.