WordPress函数has_term()判断当前页面是否带有自定义分类

描述:

判断当前页面是否带有自定义分类

用法:

<?php has_term( $term, $taxonomy, $post ) ?>

参数:

$term

(string|integer|array) (可选) 要检查的自定义分类“name/term_id/slug”或它们的数组。

默认值: ”

$taxonomy

(string) (必填) 自定义分类名称

默认值: ”

$post

(integer|object) (可选) 发布以检查而不是当前发布。

默认值: null

示例:

if( has_term( 'jazz', 'genre' ) ) {
    // do something
}

源文件:

function has_term( $term = '', $taxonomy = '', $post = null ) {
    $post = get_post( $post );
 
    if ( ! $post ) {
        return false;
    }
 
    $r = is_object_in_term( $post->ID, $taxonomy, $term );
    if ( is_wp_error( $r ) ) {
        return false;
    }
 
    return $r;
}