函数文档

get_the_taxonomies()

💡 云策文档标注

概述

get_the_taxonomies() 函数用于检索与文章关联的所有分类法,返回包含分类法名称和链接的数组。该函数可在循环内使用,支持自定义输出模板。

关键要点

  • 函数返回一个字符串数组,每个元素对应一个分类法及其术语列表。
  • 参数 $post 可选,默认为全局 $post,可接受文章 ID 或 WP_Post 对象。
  • 参数 $args 可选,用于自定义输出格式,包括 template 和 term_template。
  • template 参数定义分类法标签和术语列表的显示模板,默认值为 "Label: Terms."。
  • term_template 参数定义单个术语的显示模板,默认链接到术语归档页。
  • 函数内部使用 get_object_taxonomies() 获取分类法,并缓存术语以提高性能。

代码示例

// 示例:获取当前文章的分类法
$taxonomies = get_the_taxonomies();

// 示例:自定义模板参数
$args = array(
    'template' => '%s: %l',
    'term_template' => '%2$s'
);
$taxonomies = get_the_taxonomies( 0, $args );

注意事项

  • 函数在无文章时返回空数组,需检查返回值以避免错误。
  • 可通过 wp_sprintf() 过滤器自定义输出片段,如用户贡献笔记中所示。
  • 相关函数包括 the_taxonomies() 用于直接显示分类法。

📄 原文内容

Retrieves all taxonomies associated with a post.

Description

This function can be used within the loop. It will also return an array of the taxonomies with links to the taxonomy and name.

Parameters

$postint|WP_Postoptional
Post ID or WP_Post object. Default is global $post.
$argsarrayoptional
Arguments about how to format the list of taxonomies.

  • template string
    Template for displaying a taxonomy label and list of terms.
    Default is “Label: Terms.”
  • term_template string
    Template for displaying a single term in the list. Default is the term name linked to its archive.

Default:array()

Return

string[] List of taxonomies.

Source

function get_the_taxonomies( $post = 0, $args = array() ) {
	$post = get_post( $post );

	$args = wp_parse_args(
		$args,
		array(
			/* translators: %s: Taxonomy label, %l: List of terms formatted as per $term_template. */
			'template'      => __( '%s: %l.' ),
			'term_template' => '<a href="%1$s">%2$s</a>',
		)
	);

	$taxonomies = array();

	if ( ! $post ) {
		return $taxonomies;
	}

	foreach ( get_object_taxonomies( $post ) as $taxonomy ) {
		$t = (array) get_taxonomy( $taxonomy );
		if ( empty( $t['label'] ) ) {
			$t['label'] = $taxonomy;
		}
		if ( empty( $t['args'] ) ) {
			$t['args'] = array();
		}
		if ( empty( $t['template'] ) ) {
			$t['template'] = $args['template'];
		}
		if ( empty( $t['term_template'] ) ) {
			$t['term_template'] = $args['term_template'];
		}

		$terms = get_object_term_cache( $post->ID, $taxonomy );
		if ( false === $terms ) {
			$terms = wp_get_object_terms( $post->ID, $taxonomy, $t['args'] );
		}
		$links = array();

		foreach ( $terms as $term ) {
			$links[] = wp_sprintf( $t['term_template'], esc_attr( get_term_link( $term ) ), $term->name );
		}
		if ( $links ) {
			$taxonomies[ $taxonomy ] = wp_sprintf( $t['template'], $t['label'], $links, $terms );
		}
	}
	return $taxonomies;
}

Changelog

Version Description
2.5.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    For a plugin that is extracting words for a static search, you can add a filter to remove the taxonomy name:

    add_filter( 'wp_sprintf', function( $fragment ) {
    	$fragment = ( '%z' === $fragment ) ? '' : $fragment;
    	return $fragment;
    } );

    Then in the while loop of posts, use

    $terms = get_the_taxonomies( 0, array( 'template' => '%z%l', 'term_template' => '%2$s' ) );

    to add all the terms to the content.