get_term_field

函数
get_term_field ( $field, $term, $taxonomy = '', $context = 'display' )
参数
  • (string) $field Term field to fetch.
    Required:
  • (int|WP_Term) $term Term ID or object.
    Required:
  • (string) $taxonomy Optional. Taxonomy name. Default empty.
    Required:
    Default: (empty)
  • (string) $context Optional. How to sanitize term fields. Look at sanitize_term_field() for available options. Default 'display'.
    Required:
    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.
相关
  • sanitize_term_field()
定义位置
相关方法
get_post_fieldget_term_childrenget_term_feed_linkget_the_id_get_term_children
引入
2.3.0
弃用
-

get_term_field: 这个函数检索术语(类别、标签等)对象的一个特定字段。它需要三个参数:术语对象或ID,要检索的字段,以及是否返回单个值或一个数组值。它返回请求的术语字段值。

获得经过净化的术语字段。

该函数是出于上下文的原因和使用的简单性。

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

常见问题

FAQs
查看更多 >