
Display fields on single posts of custom taxonomy term WordPress?
To display Advanced Custom Fields (ACF) fields on single posts of a custom taxonomy term in WordPress, you can use the following code in your single post template file:
<?php // Get the current taxonomy term $term = get_the_terms(get_the_ID(), 'taxonomy_name'); // Check if the current post is associated with a term if($term) { // Get the first term object $term = array_shift($term); // Get the term ID $term_id = $term->term_id; // Get the custom field values $field_1 = get_field('field_1', 'taxonomy_name_' . $term_id); $field_2 = get_field('field_2', 'taxonomy_name_' . $term_id); // Do something with the values echo 'Field 1 value: ' . $field_1 . '<br>'; echo 'Field 2 value: ' . $field_2 . '<br>'; } ?>
Note that you’ll need to replace taxonomy_name with the actual name of your taxonomy, and replace field_1 and field_2 with the actual names of your ACF fields.
In this example, the get_the_terms function is used to get the taxonomy terms associated with the current post, and the get_field function is used to retrieve the values of the custom fields for the first term. The get_the_ID function is used to get the ID of the current post. The array_shift function is used to get the first term object from the array of terms, as a post can be associated with multiple terms of the same taxonomy.