返回介绍

get_object_taxonomies()

发布于 2017-09-10 23:38:52 字数 7705 浏览 1052 评论 0 收藏 0

get_object_taxonomies( array|string|WP_Post $object,  string $output = 'names' )

Return 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' )

参数

$object

(array|string|WP_Post) (Required) Name of the type of taxonomy object, or an object (row from posts)

$output

(string) (Optional) The type of output to return in the array. Accepts either taxonomy 'names' or 'objects'.

Default value: 'names'


返回值

(array) The names of all taxonomy of $object_type.


源代码

File: wp-includes/taxonomy.php

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

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

	$object = (array) $object;

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

	return $taxonomies;
}

更新日志

Versiondescription
2.3.0Introduced.

相关函数

Uses

  • wp-includes/media.php: get_attachment_taxonomies()

Used By

  • wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php: WP_REST_Posts_Controller::prepare_links()
  • wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php: WP_REST_Posts_Controller::get_item_schema()
  • wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php: WP_REST_Posts_Controller::get_collection_params()
  • wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php: WP_REST_Posts_Controller::prepare_item_for_response()
  • wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php: WP_REST_Posts_Controller::handle_terms()
  • wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php: WP_REST_Posts_Controller::check_assign_terms_permission()
  • wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php: WP_REST_Posts_Controller::get_items()
  • wp-includes/rest-api/endpoints/class-wp-rest-taxonomies-controller.php: WP_REST_Taxonomies_Controller::get_items_permissions_check()
  • wp-includes/rest-api/endpoints/class-wp-rest-taxonomies-controller.php: WP_REST_Taxonomies_Controller::get_items()
  • wp-includes/rest-api/endpoints/class-wp-rest-post-types-controller.php: WP_REST_Post_Types_Controller::prepare_item_for_response()
  • wp-includes/class-wp-post-type.php: WP_Post_Type::unregister_taxonomies()
  • wp-includes/post.php: wp_queue_posts_for_term_meta_lazyload()
  • wp-admin/includes/export.php: wxr_post_taxonomy()
  • wp-admin/includes/template.php: get_inline_data()
  • wp-admin/includes/post.php: bulk_edit_posts()
  • wp-admin/includes/class-wp-posts-list-table.php: WP_Posts_List_Table::inline_edit()
  • wp-admin/includes/class-wp-posts-list-table.php: WP_Posts_List_Table::get_columns()
  • wp-includes/class-wp-query.php: WP_Query::get_posts()
  • wp-includes/taxonomy.php: update_object_term_cache()
  • wp-includes/taxonomy.php: get_the_taxonomies()
  • wp-includes/taxonomy.php: get_post_taxonomies()
  • wp-includes/taxonomy.php: is_object_in_taxonomy()
  • wp-includes/taxonomy.php: clean_object_term_cache()
  • wp-includes/nav-menu-template.php: _wp_menu_item_classes_by_context()
  • wp-includes/media.php: get_attachment_taxonomies()
  • wp-includes/post.php: _update_term_count_on_transition_post_status()
  • wp-includes/post.php: wp_delete_attachment()
  • wp-includes/post.php: wp_delete_post()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::_insert_post()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::_prepare_post()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::_prepare_post_type()
  • Show 26 more used by Hide more used by

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note Contributed by Codex

    Taxonomy names for post type

    
    <?php 
    	$taxonomy_names = get_object_taxonomies( 'post' );
    	print_r( $taxonomy_names);
    ?>
    

    will typically output:

    
    Array
    (
    	[0] => category
    	[1] => post_tag
    	[2] => post_format
    )
    
  2. Taxonomy objects for post type
    If the $output parameter is 'objects', taxonomy objects will be returned as described in get_taxonomies().

    
    <?php 
    	$taxonomy_objects = get_object_taxonomies( 'post', 'objects' );
    	print_r( $taxonomy_objects);
    ?>
    

    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
            (
                ....
            )
    )
    

    Taxonomy names for post object
    To get the taxonomies for the current post, the current post object can be passed instead of the post type.

    
    <?php 
    add_action('wp_head','wpdocs_output_current_post_taxonomies');
    
    /**
     * Output taxonomies for the current post
     */
    function wpdocs_output_current_post_taxonomies(){
    	global $post;
    
    	$taxonomy_names = get_object_taxonomies( $post );
    	print_r( $taxonomy_names );
    }
    ?>
    

    will output

    
    Array
    (
        [0] => category
        [1] => post_tag
        [2] => post_format
    )
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文