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

描述:

获取文章的分类法数组

用法:

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

参数:

$post_id

(integer) (可选) 文章ID

默认值: 0

$taxonomy

(string|array) (可选) 检索分类法。默认为post_tag。

默认值: 'post_tag'

$args

(array) (可选) 覆盖默认值

默认值: array

示例:

//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 ;

源文件:

/**
 * 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;
}

 

分享到:
赞(0)

评论抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址