描述
检索帖子的条款。$args数组只有一个已识别的元素,称为“ fields ”,默认设置为“ all ”。其他值可以是“ 名称 ”或“ ids ”。在wp_get_object_terms()中还有其他默认值可以被覆盖。
用法
<?php $terms = wp_get_post_terms( $post_id, $taxonomy, $args ); ?>
参数
$post_id
(整数)(可选)帖子ID
默认值:0
$taxonomy
(string | array)(可选)用于检索术语的分类法。默认为post_tag。
默认值:'post_tag'
$args
(array)(可选)覆盖默认值
默认值:数组
默认参数
$args = array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'all');
返回值
(array| WP_Error)
一系列分类术语,如果没有找到任何术语,则为空数组。如果$ taxonomy不存在,则为WP_Error 。有关详细信息,请参阅is_wp_error()。
返回对象中的变量
term_id (int)
该术语本身的id
name (string)
术语名称
slug (string)
从名称中产生的一个。子
term_group (int)
父项的term_id(也存储为“父”)
term_taxonomy_id(int)
该术语属于的分类标识
taxonomy (string)
该术语属于的分类学名称
description (string)
分类描述
parent (int)
父项的term_id(也存储为'term_group')
count(int)
该术语的使用次数
例子
//返回“my_taxonomy”的所有术语项
$ term_list = wp_get_post_terms($ post-> ID,'my_taxonomy',array(“fields”=>“all”));
的print_r($ term_list);
//返回“my_taxonomy”的术语名称数组
$ term_list = wp_get_post_terms($ post-> ID,'my_taxonomy',array(“fields”=>“names”));
的print_r($ term_list);
//返回“my_taxonomy”的术语ID的数组
$ term_list = wp_get_post_terms($ post-> ID,'my_taxonomy',array(“fields”=>“ids”));
的print_r($ term_list);
//回显一个值 - $ term_list是一个对象数组。
您必须先选择一个//数组条目,然后才能引用其属性(字段)。
$ term_list = wp_get_post_terms($ post-> ID,'my_taxonomy',array(“fields”=>“all”));
echo $ term_list [0] - > description;
//如果一个特定的数组值存在于一个post
$ term_list = wp_get_post_terms($ post-> ID,'product_features',array(“fields”=>“all”))
foreach($ term_list as $ term_single){
echo $ term_single-> slug; //做一些事情在这里
} //如果一个特定的数组值存在于一个post $ term_list = wp_get_post_terms($ post-> ID,'product_features',array(“fields”=>“all”))foreach($ term_list as $ term_single){ echo $ term_single-> slug; //做一些事情在这里} //如果一个特定的数组值存在于一个post $ term_list = wp_get_post_terms($ post-> ID,'product_features',array(“fields”=>“all”))foreach($ term_list as $ term_single){ echo $ term_single-> slug; //做一些事情在这里}
返回
返回对象的示例
Array
(
[0] => WP_Term Object
(
[term_id] => 145
[name] =>示例类别
[slug] => example-cat
[term_group] => 0
[term_taxonomy_id] => 145
[taxonomy] => adcpt_categories
[ description] =>
[parent] => 0
[count] => 2
[filter] => raw
)
)

