函数原型
get_term_field( string $field, int|WP_Term $term, string $taxonomy = ”, string $context = ‘display’ ): string|int|null|WP_Error
函数描述
Gets sanitized term field.
是否弃用
未弃用
函数参数
-
$field
string
Required - Term field to fetch.
-
$term
int|WP_Term
Required - Term ID or object.
-
$taxonomy
string
Optional - Taxonomy name.
Default:
'' -
$context
string
Optional - How to sanitize term fields. Look at sanitize_term_field() for available options.
Default'display'.More Arguments from sanitize_term_field( … $context )
Context in which to sanitize the term field.
Accepts'raw','edit','db','display','rss','attribute', or'js'. Default'display'.
Default:
'display'
函数返回值
string|int|null|WP_Error Will return an empty string if $term is not an object or if $field is not set in $term.
函数位置
File: wp-includes/taxonomy.php.
函数源码
function get_term_field( $field, $term, $taxonomy = '', $context = 'display' ) {
$term = get_term( $term, $taxonomy );
if ( is_wp_error( $term ) ) {
return $term;
}
if ( ! is_object( $term ) ) {
return '';
}
if ( ! isset( $term->$field ) ) {
return '';
}
return sanitize_term_field( $field, $term->$field, $term->term_id, $term->taxonomy, $context );
}
源码链接
变更日志
| Version | Description |
|---|---|
| 4.4.0 | The $taxonomy parameter was made optional. $term can also now accept a WP_Term object. |
| 2.3.0 | Introduced. |

