函数文档

get_object_taxonomies()

💡 云策文档标注

概述

get_object_taxonomies() 函数用于获取注册到指定对象或对象类型(如文章对象或文章类型名称)的分类法名称或对象。它支持多种输入格式,并可通过参数控制输出类型。

关键要点

  • 函数返回指定对象类型(如 'post')关联的分类法名称或对象数组。
  • 参数 $object_type 可以是字符串、字符串数组或 WP_Post 对象,用于指定分类法对象类型。
  • 参数 $output 可选,控制输出类型:'names'(默认)返回名称数组,'objects' 返回 WP_Taxonomy 对象数组。
  • 函数内部处理附件类型,并基于全局 $wp_taxonomies 进行匹配。
  • 常用于获取文章类型的分类法,如 'category' 和 'post_tag'。

代码示例

// 获取文章类型的分类法名称(默认输出)
$taxonomies = get_object_taxonomies( 'post' );
// 结果:Array( 'category', 'post_tag' )

// 获取文章类型的分类法对象
$taxonomies = get_object_taxonomies( 'post', 'objects' );
// 返回包含 category、post_tag 等对象的数组

// 传递文章对象获取分类法
global $post;
$taxonomies = get_object_taxonomies( $post );
// 结果类似:Array( 'category', 'post_tag', 'post_format' )

注意事项

  • 函数自 WordPress 2.3.0 引入,兼容性良好。
  • 输出数组格式取决于 $output 参数:'names' 时为索引数组,'objects' 时为关联数组。
  • 支持批量处理多个对象类型,无需循环调用。
  • 相关函数包括 get_attachment_taxonomies() 和 get_taxonomies(),用于特定场景。

📄 原文内容

Returns the names or objects of the taxonomies which are registered for the requested object or object type, such as a post object or post type name.

Description

Example:

$taxonomies = get_object_taxonomies( 'post' );

This results in:

Array( 'category', 'post_tag' )

Parameters

$object_typestring|string[]|WP_Postrequired
Name of the type of taxonomy object, or an object (row from posts).
$outputstringoptional
The type of output to return in the array. Accepts either 'names' or 'objects'. Default 'names'.

Return

string[]|WP_Taxonomy[] The names or objects of all taxonomies of $object_type.

Source

function get_object_taxonomies( $object_type, $output = 'names' ) {
	global $wp_taxonomies;

	if ( is_object( $object_type ) ) {
		if ( 'attachment' === $object_type->post_type ) {
			return get_attachment_taxonomies( $object_type, $output );
		}
		$object_type = $object_type->post_type;
	}

	$object_type = (array) $object_type;

	$taxonomies = array();
	foreach ( (array) $wp_taxonomies as $tax_name => $tax_obj ) {
		if ( array_intersect( $object_type, (array) $tax_obj->object_type ) ) {
			if ( 'names' === $output ) {
				$taxonomies[] = $tax_name;
			} else {
				$taxonomies[ $tax_name ] = $tax_obj;
			}
		}
	}

	return $taxonomies;
}

Changelog

Version Description
2.3.0 Introduced.

User Contributed Notes

  1. Skip to note 5 content

    Taxonomy objects for post type
    If the $output parameter is 'objects', taxonomy objects will be returned as described in get_taxonomies().

    will output

    Array
    (
        [category] => stdClass Object
            (
                [hierarchical] => 1
                [update_count_callback] => 
                [rewrite] => 
                [query_var] => category_name
                [public] => 1
                [show_ui] => 1
                [show_tagcloud] => 1
                [_builtin] => 1
                [labels] => stdClass Object
                    (
                        ...
                    )
                ...
                [name] => category
                [label] => Categories
            )
        [post_tag] => stdClass Object
            (
                ...
            )
        [post_format] => stdClass Object
            (
                ....
            )
    )