I have custom field my_cf
for Taxonomy/Term. How can I get and output value with custom field for taxonomy/term?
I’ve tried using:
$variable = get_field('my_cf', 'basic');
echo $variable;
where basic – name for my taxonomy. But this doesn’t work.
Any suggestions?
I can’t really explain it any better than the ACF documentation page I posted in the comments:
So if your custom field is
my_cf
, and your taxonomy name isbasic
(not term name) and the term ID within your taxonomy is 42, then you need:Is your field data stored in wp_options? If so…
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.Simply use
<?php get_taxonomy_terms_custom_fields ('basic'); ?>
replacingbasic
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!