WordPress获取文章类型注册的分类法名称函数get_object_taxonomies

WordPress教程 1272

创建了自定义文章类型以及注册文章类型的分类法,如果需要在前台页面获取自定义文章类型所属的taxonomy分类法,可以使用自定义文章类型的名称,通过get_object_taxonomies()函数返回注册的分类法的名称或对象。

代码结构

1
get_object_taxonomies( string|string[]|WP_Post $object, string $output = 'names' )
作用

返回为请求的对象或对象类型注册的分类法的名称或对象,例如post对象或post类型名称。

参数
  • $object – (string | string[]|WP_Post)(必需)分类对象或对象类型的名称(posts中的行),如post
  • $output – (string)(可选)要在数组中返回的输出类型。接受“names”或“objects”,默认值为“names”

示例

$output为默认值“names”时
1
$taxonomies = get_object_taxonomies( 'post' );

返回值

1
Array( 'category', 'post_tag' )
$output为“objects”时
1
2
$taxonomy_objects = get_object_taxonomies( 'post', 'objects' );
    print_r( $taxonomy_objects);

返回值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
        (
            ....
        )
)
获取当前页面分类法的名称
1
2
$taxonomies = get_object_taxonomies(get_post_type());
print_r($taxonomies[1]);

源文件

位置:wp-includes/taxonomy.php

WordPress官网:https://developer.wordpress.org/reference/functions/get_object_taxonomies/

精品推荐: