当前浏览:首页 / WordPress函数 / get_post_type_object()

get_post_type_object( string $post_type )

按名称检索文章类型对象

object

post_type 文章类型more...


描述

另见


参数

$post_type

(string)(必填) 已注册的文章类型的名称。


返回

(WP_Post_Type|null) WP_Post_Type对象(如果存在),否则为null。



源码

查看源码 官方文档


更新日志

版本描述
4.6.0返回的对象现在是WP_Post_Type的实例。
3.0.0开始引入

使用示例

  • 示例1

    举例

    $obj = get_post_type_object( 'post' );
    echo $obj->labels->singular_name;
    

    从技术上讲,这同等于

    global $wp_post_types;
    $obj = $wp_post_types['post'];
    echo $obj->labels->singular_name; 
    

    print_r( $obj )可能会返回以下内容:

    stdClass Object
    (
    	[labels] => stdClass Object
    		(
    			[name] => Posts
    			[singular_name] => Post
    			[add_new] => Add New
    			[add_new_item] => Add New Post
    			[edit_item] => Edit Post
    			[new_item] => New Post
    			[view_item] => View Post
    			[search_items] => Search Posts
    			[not_found] => No posts found
    			[not_found_in_trash] => No posts found in Trash
    			[parent_item_colon] => 
    		)
    	[description] => 
    	[publicly_queryable] => 1
    	[exclude_from_search] => 
    	[_builtin] => 1
    	[_edit_link] => post.php?post=%d
    	[capability_type] => post
    	[hierarchical] => 
    	[public] => 1
    	[rewrite] => 
    	[query_var] => 
    	[register_meta_box_cb] => 
    	[taxonomies] => Array
    		(
    		)
    	[show_ui] => 1
    	[menu_position] => 
    	[menu_icon] => 
    	[permalink_epmask] => 1
    	[can_export] => 1
    	[show_in_nav_menus] => 1
    	[name] => post
    	[cap] => stdClass Object
    		(
    			[edit_post] => edit_post
    			[edit_posts] => edit_posts
    			[edit_others_posts] => edit_others_posts
    			[publish_posts] => publish_posts
    			[read_post] => read_post
    			[read_private_posts] => read_private_posts
    			[delete_post] => delete_post
    		)
    	[label] => Posts
    )
    

    假设我们有post类型“certification”,我们可以这样做:

    $obj = get_post_type_object( 'certification' );
    

    print_r( $obj )可能会返回以下内容:

    stdClass Object
    (
    	[labels] => stdClass Object
    		(
    			[name] => Certification
    			[singular_name] => Certification
    			[add_new] => Add New
    			[add_new_item] => Add New Certification
    			[edit_item] => Edit Certification
    			[new_item] => New Page
    			[view_item] => View Certification
    			[search_items] => Search Certification
    			[not_found] => Not found
    			[not_found_in_trash] => Not found in Trash
    			[parent_item_colon] => Parent Certification:
    			[all_items] => All Certifications
    			[menu_name] => Certifications
    			[update_item] => Update Certification
    			[name_admin_bar] => Certification
    		)
    	[description] => Certifications
    	[public] => 1
    	[hierarchical] => 1
    	[exclude_from_search] => 
    	[publicly_queryable] => 1
    	[show_ui] => 1
    	[show_in_menu] => 
    	[show_in_nav_menus] => 1
    	[show_in_admin_bar] => 1
    	[menu_position] => 5
    	[menu_icon] => dashicons-welcome-widgets-menus
    	[capability_type] => post
    	[map_meta_cap] => 1
    	[register_meta_box_cb] => 
    	[taxonomies] => Array
    		(
    			[0] => objective
    		)
    	[has_archive] => 1
    	[rewrite] => Array
    		(
    			[slug] => certification
    			[with_front] => 1
    			[pages] => 1
    			[feeds] => 1
    			[ep_mask] => 1
    		)
    	[query_var] => certification
    	[can_export] => 1
    	[delete_with_user] => 
    	[_builtin] => 
    	[_edit_link] => post.php?post=%d
    	[label] => Certification
    	[name] => certification
    	[cap] => stdClass Object
    		(
    			[edit_post] => edit_post
    			[read_post] => read_post
    			[delete_post] => delete_post
    			[edit_posts] => edit_posts
    			[edit_others_posts] => edit_others_posts
    			[publish_posts] => publish_posts
    			[read_private_posts] => read_private_posts
    			[read] => read
    			[delete_posts] => delete_posts
    			[delete_private_posts] => delete_private_posts
    			[delete_published_posts] => delete_published_posts
    			[delete_others_posts] => delete_others_posts
    			[edit_private_posts] => edit_private_posts
    			[edit_published_posts] => edit_published_posts
    			[create_posts] => edit_posts
    		)
    )
    

    请注意,对象的属性名与register_post_type()函数的预期参数略有不同。