函数文档

get_term_meta()

💡 云策文档标注

概述

get_term_meta() 函数用于检索分类法术语的元数据。它基于 get_metadata() 实现,支持指定术语 ID、元键和返回类型。

关键要点

  • 参数:$term_id(必需,术语 ID)、$key(可选,元键,默认为空返回所有键)、$single(可选,是否返回单个值,默认为 false)
  • 返回值:根据 $single 参数返回数组或单个值;无效 $term_id 返回 false;非序列化值以字符串形式返回(如 false 为空字符串,true 为 '1')
  • 相关函数:get_metadata() 用于通用元数据检索
  • 版本:自 WordPress 4.4.0 引入

代码示例

// 在存档或分类模板中使用,检索单个元值
$term_image = get_term_meta( get_queried_object_id(), '_myprefix_term_image', true);

// 检索所有元键值对并遍历输出
$term_vals = get_term_meta($term_id);
foreach($term_vals as $key=>$val){
    echo $key . ' : ' . $val[0] . '';
}

📄 原文内容

Retrieves metadata for a term.

Parameters

$term_idintrequired
Term ID.
$keystringoptional
The meta key to retrieve. By default, returns data for all keys. Default empty.
$singlebooloptional
Whether to return a single value.
This parameter has no effect if $key is not specified.

Default:false

Return

mixed An array of values if $single is false.
The value of the meta field if $single is true.
False for an invalid $term_id (non-numeric, zero, or negative value).
An empty array if a valid but non-existing term ID is passed and $single is false.
An empty string if a valid but non-existing term ID is passed and $single is true.
Note: Non-serialized values are returned as strings:

  • false values are returned as empty strings ('')
  • true values are returned as '1'
  • numbers are returned as strings Arrays and objects retain their original type.

Source

function get_term_meta( $term_id, $key = '', $single = false ) {
	return get_metadata( 'term', $term_id, $key, $single );
}

Changelog

Version Description
4.4.0 Introduced.

User Contributed Notes