Support

Account

Home Forums Front-end Issues Display custom taxonomy name

Solved

Display custom taxonomy name

    • tacaha5

    • November 5, 2020 at 4:43 pm

    I have created a custom post type called “Researchers”. There is a custom taxonomy field named “modeling types” which I’m using multi-select. At first I was only able to display the taxonomy ID. I got some help in re-writing the code to display the taxonomy name(s). But the field now returns “Uncategorized”. Can someone look at my code and tell me what I’m doing wrong. I’m new to php.

    Modeling Type: <?php $modelingType = get_field('modeling_types'); $term = get_term($modelingType); echo esc_html($term->name); ?>

    The output looks like this:
    Modeling Type: Uncategorized

    Also, the return value is set to Term ID. However when I set it to Term Object, the Modeling Type: Uncategorized doesn’t show at all.

    Thank you for any assistance.

  • A taxonomy field returns a single term of an array of terms dependent on the settings for the field. See code examples here for how to use a taxonomy field https://www.advancedcustomfields.com/resources/taxonomy/

    Your issue with Uncategorized is that in your code get_term() requires you to pass the taxonomy name or “category” is assumed, meaning the default category taxonomy for posts.

    • tacaha5

    • November 19, 2020 at 9:02 am

    Thank you for your assistance. This is what I have now. The “Uncategorized” word is now gone, but the field is displaying blank. I’m missing something and just don’t know where.
    Modeling Type: <?php $modelingType = get_field('modeling_types'); $term = get_term($modelingType, $taxonomy = 'modeling_types'); echo esc_html($term->name); ?>

    This was the display before:Modeling Type: Uncategorized
    This is the display now: Modeling Type:

    Thank you!

    • tacaha5

    • November 19, 2020 at 12:38 pm

    Actually this solved my issue.
    <?php the_terms( $post->ID, 'fam_type', ' ', ' | ', ' ' ); ?>

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.

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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.