如果语句显示后置元

时间:2019-05-01 23:23:43

标签: php wordpress if-statement conditional

我有一个名为talent_cat的分类法,属于上述帖子的类别,它具有4个值:modelos, atores, musica, djs,并且在每个单独的talent_cat中都有这些post meta keys:
talent_id_1495127471815代表modelos
talent_id_1495127471816代表atores
talent_id_1495127471817代表musica
{{1} } talent_id_1495127471818

如何根据该帖子的djs建立一个条件来显示正确的post meta?因此,如果帖子属于talent_cat,它将返回talent_cat = modelos,而不返回其他值。

我尝试了这个但没有成功:

talent_id_1495127471815

谢谢。

1 个答案:

答案 0 :(得分:2)

函数get_the_term_list返回HTML,但是您需要获取一个数组或对象来检查您的语句。您需要使用get_the_terms函数。您的代码应如下所示:

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;
    }
}