【问题标题】:If statement to display Post metaIf 语句显示 Post 元数据
【发布时间】:2019-05-01 23:23:43
【问题描述】:

我有一个名为 talent_cat 的分类法,它是所述帖子的类别,有 4 个值:modelos, atores, musica, djs 和每个人内部 talent_cat 有这些 post meta keys:
talent_id_1495127471815 用于 modelostalent_id_1495127471816 for atores
talent_id_1495127471817 for musica
talent_id_1495127471818 for djs

如何根据所述帖子的talent_cat 构建条件以显示正确的post meta?因此,如果帖子属于 talent_cat = modelos,它会返回 talent_id_1495127471815 而不是其他值。

我试过了,但没有成功:

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

谢谢。

【问题讨论】:

    标签: php wordpress if-statement conditional


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-01
      • 2014-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-21
      相关资源
      最近更新 更多