函数文档

get_tags()

💡 云策文档标注

概述

get_tags() 函数用于检索所有文章标签,基于 get_terms() 实现,默认操作 'post_tag' 分类法。它返回标签对象数组、计数或错误,并包含一个过滤器钩子用于自定义输出。

关键要点

  • 函数签名:get_tags( $args = '' ),参数 $args 可选,可传递数组或字符串以定制查询。
  • 默认参数:taxonomy 默认为 'post_tag',通过 wp_parse_args() 合并用户参数。
  • 返回值:WP_Term[] 数组、整数计数或 WP_Error 对象,具体取决于查询结果。
  • 过滤器钩子:apply_filters( 'get_tags', $tags, $args ),允许在返回前修改标签数据。
  • 相关函数:依赖 get_terms() 进行实际查询,wp_parse_args() 处理参数,apply_filters() 应用过滤器。

代码示例

$tags = get_tags();
$html = '';
foreach ( $tags as $tag ) {
    $tag_link = get_tag_link( $tag->term_id );
    $html .= "<a href='{$tag_link}' title='{$tag->name} Tag' class='{$tag->slug}'>";
    $html .= "{$tag->name}";
    $html .= "</a>";
}
$html .= '';
echo $html;

注意事项

  • 参数 $args 支持从 WP_Term_Query 继承的选项,如 'orderby'、'hide_empty' 等,用于高级查询控制。
  • 如果查询结果为空,函数返回空数组,避免返回 null 或错误值。
  • 使用过滤器 'get_tags' 时,需注意回调函数应正确处理 $tags 和 $args 参数。

📄 原文内容

Retrieves all post tags.

Parameters

$argsstring|arrayoptional
Arguments to retrieve tags. See get_terms() for additional options.

  • taxonomy string
    Taxonomy to retrieve terms for. Default 'post_tag'.

More Arguments from get_terms( … $args )

Array or string of arguments. See WP_Term_Query::__construct() for information on accepted arguments.

Return

WP_Term[]|int|WP_Error Array of 'post_tag' term objects, a count thereof, or WP_Error if any of the taxonomies do not exist.

Source

function get_tags( $args = '' ) {
	$defaults = array( 'taxonomy' => 'post_tag' );
	$args     = wp_parse_args( $args, $defaults );

	$tags = get_terms( $args );

	if ( empty( $tags ) ) {
		$tags = array();
	} else {
		/**
		 * Filters the array of term objects returned for the 'post_tag' taxonomy.
		 *
		 * @since 2.3.0
		 *
		 * @param WP_Term[]|int|WP_Error $tags Array of 'post_tag' term objects, a count thereof,
		 *                                     or WP_Error if any of the taxonomies do not exist.
		 * @param array                  $args An array of arguments. See <a href="https://developer.wordpress.org/reference/functions/get_terms/">get_terms()</a>.
		 */
		$tags = apply_filters( 'get_tags', $tags, $args );
	}

	return $tags;
}

Hooks

apply_filters( ‘get_tags’, WP_Term[]|int|WP_Error $tags, array $args )

Filters the array of term objects returned for the ‘post_tag’ taxonomy.

Changelog

Version Description
2.3.0 Introduced.

User Contributed Notes

  1. Skip to note 4 content

    Displays a list of tags with links to each one and a specific class for each tag:

    $tags = get_tags();
    $html = '<div class="post_tags">';
    foreach ( $tags as $tag ) {
    	$tag_link = get_tag_link( $tag->term_id );
    			
    	$html .= "<a href='{$tag_link}' title='{$tag->name} Tag' class='{$tag->slug}'>";
    	$html .= "{$tag->name}</a>";
    }
    $html .= '</div>';
    echo $html;

  2. Skip to note 6 content

    Display all tags in list format with links.

    $tags = get_tags(array('get'=>'all'));
    	$output .= '<ul class="tag-cloud-list">';
    		if($tags) {
    		foreach ($tags as $tag):
    		$output .= '<li><a href="'. get_term_link($tag).'">'. $tag->name .'</a></li>';
    		endforeach;
    		} else {
    		_e('No tags created.', 'text-domain');
    		}
    	$output .= '</ul>';
    return $output;