wp_get_post_tags()获取文章的标签Tags信息

目录

描述

Retrieve a list of tags for a specific post.

wp_get_post_tags() 描述

用法

<?php wp_get_post_tags$post_id$args ?>

wp_get_post_tags() 用法

参数

$post_id

(integer) (可选) The Post ID.

默认值: 0

$args

(array) (可选) Overwrite the defaults. See wp_get_object_terms() for a list of valid options that can be passed.

默认值: array

wp_get_post_tags() 参数

返回值

(array) 

List of post tags.

wp_get_post_tags() 返回值

示例

For a post with tags tag2, tag5 and tag6, the code

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
$t = wp_get_post_tags($post->ID);
print_r($t);

Displays

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
Array
(
   [0] => stdClass Object
       (
           [term_id] => 4
           [name] => tag2
           [slug] => tag2
           [term_group] => 0
           [term_taxonomy_id] => 4
           [taxonomy] => post_tag
           [description] => 
           [parent] => 0
           [count] => 7
       )

   [1] => stdClass Object
       (
           [term_id] => 7
           [name] => tag5
           [slug] => tag5
           [term_group] => 0
           [term_taxonomy_id] => 7
           [taxonomy] => post_tag
           [description] => 
           [parent] => 0
           [count] => 6
       )

   [2] => stdClass Object
       (
           [term_id] => 16
           [name] => tag6
           [slug] => tag6
           [term_group] => 0
           [term_taxonomy_id] => 16
           [taxonomy] => post_tag
           [description] => 
           [parent] => 0
           [count] => 2
       )

)

To get a list of only the tag IDs for a particular post:

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
global $post;
$tag_ids = wp_get_post_tags( $post->ID, array( 'fields' => 'ids' ) );

and assuming the same dataset as the first example, $tag_ids would contain

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
[4, 7, 16]

wp_get_post_tags() 示例

注意

历史

添加于 版本: 2.3.0

wp_get_post_tags() 历史

源文件

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

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Retrieve the tags 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.3.0
 *
 * @param int   $post_id Optional. The Post ID. Does not default to the ID of the
 *                       global $post. Defualt 0.
 * @param array $args Optional. Overwrite the defaults
 * @return array List of post tags.
 */
function wp_get_post_tags( $post_id = 0, $args = array() ) {
	return wp_get_post_terms( $post_id, 'post_tag', $args);
}

wp_get_post_tags() 源文件