Home Forums Front-end Issues Display custom taxonomy name


Display custom taxonomy name

  • 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

    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.

  • 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!

  • 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.