I use CMB2 to set up custom fields, and the logic is not so different from ACF in many cases. For my specific use case I've created a very simple but flexible function in order to make a few checks for the taxonomy before displaying the custom field value.
Considering one have created a custom field named my_cf
for let's say a taxonomy named basic as per your example, the following function might help answer your question and perhaps extend your custom field's usage a bit.
function get_taxonomy_terms_custom_fields( $taxonomy = '' ) {
global $post;
$terms = get_the_terms( $post->ID, $taxonomy );
// Check if we have a taxonomy and that it is valid. If not, return false
if ( !$taxonomy )
return false;
// Sanitize the taxonomy input
$taxonomy = filter_var( $taxonomy, FILTER_SANITIZE_STRING );
// keep playing safe
if ( !taxonomy_exists( $taxonomy ) )
return false;
foreach ( $terms as $term ) {
// Set a variable for taxonomy term_id
$tax_term_id = $term->term_id;
$my_field = get_term_meta( $tax_term_id, 'my_cf', true );
// Make sure we do not have a WP_Error object, not really necessary, but better be safe
if ( is_wp_error( $term ) )
continue;
// escaping the returned value // esc_html(), esc_url(), esc_attr()
return esc_html($my_field);
}
}
Simply use <?php get_taxonomy_terms_custom_fields ('basic'); ?>
replacing basic
with you own taxonomy name.
The function get_taxonomy_terms_custom_fields ()
will check for the specified taxonomy and kind of loop through all categories assigned to a post, post_type and then return the custom field value if present, avoiding erros if not. It could also be extended to check for a field which produces an array() such as a repeatable field.
I hope it helps - Good Luck!
get_field
is not a WordPress core function, your question appears to be missing some information.get_field
?