0

I am new to custom taxonomies, and created a taxonomy with 3 term meta fields using the generator on wp-hasty.com

The taxonomy and new fields show up properly on the back end, and I am able to save to them.

My problem is displaying them on the front end.

I have a custom post type named "paint", and my taxonomy is named "colors", and the term meta fields are named "blue", "red", and "green". The term meta fields are regular text fields with text in them.

I would like to display the contents of "blue", "red", and "green" on my custom post type page, if the page has the taxonomy tag colors.

Online I found get_term_meta(), but I couldn't get it right.

I appreciate all help. Please let me know if I'm not being clear enough with my question.

  • When you say "term meta fields" I'm not clear what you mean. Are these like text fields with values in them, or do you just mean that "blue", "red", and "green" are choices of colour? I'm just not clear what's supposed to be 'meta'. – Jacob Peattie Jul 16 '18 at 2:27
  • Sorry for the confusion. The term meta fields are regular text fields with text in them. – harvey Jul 16 '18 at 4:08
  • Please check this: How to show taxonomy meta on frontpage? – Michael Jul 16 '18 at 4:33
  • @Michael Thanks, but unfortunately that did not help me. – harvey Jul 16 '18 at 14:59
1

1. If you need to display this informations once. For example in the page header, use get_queried_object().

echo get_term_meta( get_queried_object()->term_id, 'blue', true );
echo get_term_meta( get_queried_object()->term_id, 'red', true );
echo get_term_meta( get_queried_object()->term_id, 'green', true );

2. If you need to display this informations in the each post, you must create a small loop.

$terms = get_the_terms($post->ID, 'colors');
foreach ($terms as $term) {
    $term_id = $term->term_id;
    echo get_term_meta( $term_id, 'blue', true );
    echo get_term_meta( $term_id, 'red', true );
    echo get_term_meta( $term_id, 'green', true );
}
  • Thanks, but I found that earlier, and I just don't know how to use it. My post type is "paint", my taxonomy is "colors" and my term meta field is "blue". How do I plug that in? – harvey Jul 17 '18 at 0:26
  • f you need to display this informations once. For example in the page header, use get_queried_object(). If you need to display this informations in the each post, you must create a small loop. Updated answer. – Michael Jul 17 '18 at 4:38
  • Your'e a life saver! Thanks so much for a clear and concise answer. – harvey Jul 18 '18 at 2:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.