wp_get_post_terms()获取文章的分类法数组

目录

描述

Retrieve the terms for a post.

There is only one default for this function, called 'fields' and by default is set to 'all'. Other values can be 'names' or 'ids'. There are other defaults that can be overridden in wp_get_object_terms().

wp_get_post_terms() 描述

用法

<?php $terms wp_get_post_terms$post_id$taxonomy$args ); ?>

wp_get_post_terms() 用法

参数

$post_id

(integer) (可选) The Post ID

默认值: 0

$taxonomy

(string|array) (可选) The taxonomy for which to retrieve terms. Defaults to post_tag.

默认值: 'post_tag'

$args

(array) (可选) Overwrite the defaults

默认值: array

Default Arguments

$args = array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'all');

wp_get_post_terms() 参数

返回值

(array|WP_Error) 

List of post tags. WP_Error if $taxonomy does not exist. See is_wp_error() for more information.

Variables in Returned Object

term_id (int) 

the id of the term itself

name (string) 

the term name

slug (string) 

a slug generated from the term name

term_group (int) 

the term_id of the parent term (also stored as 'parent')

term_taxonomy_id (int) 

the id of the taxonomy that the term belongs to

taxonomy (string) 

the name of the taxonomy that the term belongs to

description (string) 

the taxonomy description

parent (int) 

the term_id of the parent term (also stored as 'term_group')

count (int) 

the number of uses of this term

wp_get_post_terms() 返回值

示例

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
//Returns All Term Items for "my_taxonomy"
$term_list = wp_get_post_terms($post->ID, 'my_taxonomy', array("fields" => "all"));
print_r($term_list);

//Returns Array of Term Names for "my_taxonomy"
$term_list = wp_get_post_terms($post->ID, 'my_taxonomy', array("fields" => "names"));
print_r($term_list);

//Returns Array of Term ID's for "my_taxonomy"
$term_list = wp_get_post_terms($post->ID, 'my_taxonomy', array("fields" => "ids"));
print_r($term_list);

//Echo a single value - $term_list is an array of objects. You must select one of the 
// array entries before you can reference its properties (fields).
$term_list = wp_get_post_terms($post->ID, 'my_taxonomy', array("fields" => "all"));
echo $term_list[0]->description ;

wp_get_post_terms() 示例

注意

wp_get_post_terms() 注意

历史

添加于 版本: 2.8.0

wp_get_post_terms() 历史

源文件

wp_get_post_terms() 函数的代码位于 wp-includes/post.php.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Retrieve the terms for a post.
 *
 * There is only one default for this function, called 'fields' and by default
 * is set to 'all'. There are other defaults that can be overridden in
 * {@link wp_get_object_terms()}.
 *
 * @since 2.8.0
 *
 * @param int    $post_id  Optional. The Post ID. Does not default to the ID of the
 *                         global $post. Default 0.
 * @param string $taxonomy Optional. The taxonomy for which to retrieve terms. Default 'post_tag'.
 * @param array  $args     Optional. {@link wp_get_object_terms()} arguments. Default empty array.
 * @return array|WP_Error  List of post terms or empty array if no terms were found. WP_Error object
 *                         if `$taxonomy` doesn't exist.
 */
function wp_get_post_terms( $post_id = 0, $taxonomy = 'post_tag', $args = array() ) {
	$post_id = (int) $post_id;

	$defaults = array('fields' => 'all');
	$args = wp_parse_args( $args, $defaults );

	$tags = wp_get_object_terms($post_id, $taxonomy, $args);

	return $tags;
}

wp_get_post_terms() 源文件

相关