Steve Kess
Steve Kess

Reputation: 51

If statement to display Post meta

I have a taxonomy named talent_cat which is the category of said post, has 4 values: modelos, atores, musica, djs and inside each individual talent_cat there are these post meta keys:
talent_id_1495127471815 for modelos
talent_id_1495127471816 for atores
talent_id_1495127471817 for musica
talent_id_1495127471818 for djs

How can I build a conditional to show the right post meta depending on the talent_cat of said post? So if the post belongs to talent_cat = modelos it returns the talent_id_1495127471815 and not the other values.

I tried this but unsuccessfully:

$talent_data = get_the_term_list( $post->ID, 'talent_cat');
switch( $talent_data ) {
                case 'modelos':
                    echo get_post_meta(get_the_ID(), 'talent_id_1495127471815', true);
                    break;
      }

Thanks.

Upvotes: 0

Views: 70

Answers (1)

Artem
Artem

Reputation: 765

The function get_the_term_list returns HTML but you need to get an array or object to check your statement. You need to use get_the_terms function instead. Your code should look like this:

global $post;
$talent_data = get_the_terms( $post->ID, 'talent_cat');

// check for errors or empty data
if ( is_wp_error( $talent_data ) || empty( $talent_data ) ) {
    return;
}

foreach ( $talent_data as $term ) {
    // you can check for name or slug or id
    switch( $term->slug ) {
        case 'modelos':
           echo get_post_meta($post->ID, 'talent_id_1495127471815', true);
           break;
    }
}

Upvotes: 2

Related Questions