Home › Forums › Backend Issues (wp-admin) › Display ACF from specific Taxonomy
Hi. I am creating a website using CPT UI and ACF.
I have created a CPT called ‘repairs’. Inside this CPT I have created 2 taxonomies:
‘repair_type’ and ‘product_brand’.
Each post is related to a repair type (e.g. Screen replacement, battery replacement, case repair, etc.. ) and also a brand (e.g. Apple, Samsung, Sony, etc..).
Inside a taxonomy ‘repair_type’ I have a ACF field called ‘repair_price’. This field appears no problem in the admin for each repair type. My issue is that I would like to display this field in the ‘repairs’ post. I have tried modifying the single.php file to display this using:
<?php $term = get_field(‘repair_price’); ?>
<?php the_field(‘repair_price’, $term); ?> €
But the field doesn´t show. I think that maybe, as the post is linked to 2 taxonomies I may have to specify exactly which taxonomy term I need to pull the data from?
I have also tried:
$term = get_queried_object();
$price = get_field(‘repair_price’, $term);
<?php echo $price; ?>;
but this also does not work.
Any ideas. Appreciate any help
Do you have a field where you set the term in each of these taxonomies or do they use the standard WP metaboxes?
You need to get the term in the taxonomy related to the post, what you need to do that depends on if you have an ACF taxonomy field for each taxonomy on the post edit page or not.
Hi,
I don´t really understand. I set up the taxonomy using CPT UI and then added a ACF field to it for the price field. Everything else is standard. I am not using the ACF taxonomy type field.
The price field is on the taxonomy term. Before you can get that field you need to get the term to get it from.
I am asking how you set this term when you edit a “Repair” post.
Oh I see. I set it using the WO taxonomy checkboxes which appear in the right column. I am not using the ACF taxonomy selector.
Thanks
You need to get the terms as set by WP for the post
https://developer.wordpress.org/reference/functions/get_the_terms/
$terms = get_the_terms($post->ID, 'repair_type');
This will return an array containing all of the selected terms. You need to loop over these and get the field from the term
if (!empty($terms) && !is_wp_error($terms)) {
foreach ($terms as $term) {
$repair_price = get_field('repair_price[, $term);
}
}
You must be logged in to reply to this topic.
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.