^ 回到顶部
  • 人生没有定律,每个人都有自己的节奏
  • 本站wordpress建站教程均通过实践后发布,希望对你有帮助
  • 希望你的坚持,都是因为热爱,而不是因为不甘心
  • 8年wordpress建站经验,5星服务品质
  • 那些不愿意让你吃亏的人,才是真正值得你深交的人,也是值得你付出时间的人
  • 阿里云2核2g仅需86元/年

wordpress函数get_post_type_object()用法示例

get_post_type_object( string $post_type )

Retrieves a post type object by name.


描述


参数

$post_type

(string)
(Required)
The name of a registered post type.

新用户2核2g仅需86元/年    领取1153套模板    免费cdn流量包    ¥3188礼包


返回值

(WP_Post_Type|null) WP_Post_Type object if it exists, null otherwise.


源代码

File: wp-includes/post.php

function get_post_type_object( $post_type ) {
	global $wp_post_types;

	if ( ! is_scalar( $post_type ) || empty( $wp_post_types[ $post_type ] ) ) {
		return null;
	}

	return $wp_post_types[ $post_type ];
}

更新日志

Version 描述
4.6.0 Object returned is now an instance of WP_Post_Type.
3.0.0 Introduced.

相关函数

Used By

  • wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php:
    WP_REST_Revisions_Controller::delete_item_permissions_check()
  • wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php:
    WP_REST_Revisions_Controller::__construct()
  • wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php:
    WP_REST_Revisions_Controller::get_items_permissions_check()
  • wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php:
    WP_REST_Attachments_Controller::create_item_permissions_check()
  • wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php:
    WP_REST_Terms_Controller::prepare_links()
  • 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::sanitize_post_statuses()
  • wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php:
    WP_REST_Posts_Controller::check_is_post_type_allowed()
  • wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php:
    WP_REST_Posts_Controller::check_read_permission()
  • wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php:
    WP_REST_Posts_Controller::check_update_permission()
  • wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php:
    WP_REST_Posts_Controller::check_create_permission()
  • wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php:
    WP_REST_Posts_Controller::check_delete_permission()
  • wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php:
    WP_REST_Posts_Controller::prepare_item_for_database()
  • wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php:
    WP_REST_Posts_Controller::update_item_permissions_check()
  • wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php:
    WP_REST_Posts_Controller::get_item()
  • wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php:
    WP_REST_Posts_Controller::create_item_permissions_check()
  • wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php:
    WP_REST_Posts_Controller::__construct()
  • wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php:
    WP_REST_Posts_Controller::get_items_permissions_check()
  • wp-includes/rest-api/endpoints/class-wp-rest-post-types-controller.php:
    WP_REST_Post_Types_Controller::get_item()
  • wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php:
    WP_REST_Comments_Controller::check_read_post_permission()
  • wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php:
    WP_REST_Comments_Controller::prepare_links()
  • wp-includes/class-wp-customize-nav-menus.php:
    WP_Customize_Nav_Menus::print_post_type_container()
  • wp-includes/class-wp-customize-nav-menus.php:
    WP_Customize_Nav_Menus::sanitize_nav_menus_created_posts()
  • wp-includes/class-wp-customize-nav-menus.php:
    WP_Customize_Nav_Menus::ajax_insert_auto_draft_post()
  • wp-includes/customize/class-wp-customize-nav-menu-item-setting.php:
    WP_Customize_Nav_Menu_Item_Setting::get_original_title()
  • wp-includes/customize/class-wp-customize-nav-menu-item-setting.php:
    WP_Customize_Nav_Menu_Item_Setting::get_type_label()
  • wp-includes/post.php:
    unregister_post_type()
  • wp-includes/link-template.php:
    get_preview_post_link()
  • wp-includes/post.php:
    is_post_type_viewable()
  • wp-includes/admin-bar.php:
    wp_admin_bar_customize_menu()
  • wp-includes/class-wp-xmlrpc-server.php:
    wp_xmlrpc_server::_toggle_sticky()
  • wp-includes/class-wp-customize-nav-menus.php:
    WP_Customize_Nav_Menus::enqueue_scripts()
  • wp-includes/class-wp-customize-nav-menus.php:
    WP_Customize_Nav_Menus::load_available_items_query()
  • wp-admin/includes/class-wp-posts-list-table.php:
    WP_Posts_List_Table::handle_row_actions()
  • wp-admin/includes/class-wp-posts-list-table.php:
    WP_Posts_List_Table::column_title()
  • wp-admin/includes/class-wp-media-list-table.php:
    WP_Media_List_Table::column_parent()
  • wp-admin/includes/post.php:
    wp_edit_attachments_query_vars()
  • wp-admin/includes/export.php:
    export_wp()
  • wp-admin/includes/deprecated.php:
    get_editable_user_ids()
  • wp-admin/includes/dashboard.php:
    wp_dashboard_right_now()
  • wp-admin/includes/dashboard.php:
    wp_dashboard_setup()
  • wp-admin/includes/template.php:
    get_inline_data()
  • wp-admin/includes/media.php:
    get_media_item()
  • wp-admin/includes/post.php:
    _wp_post_thumbnail_html()
  • wp-admin/includes/post.php:
    _admin_notice_post_locked()
  • wp-admin/includes/post.php:
    wp_write_post()
  • wp-admin/includes/post.php:
    get_sample_permalink()
  • wp-admin/includes/post.php:
    _wp_translate_postdata()
  • wp-admin/includes/post.php:
    edit_post()
  • wp-admin/includes/post.php:
    bulk_edit_posts()
  • wp-admin/includes/ajax-actions.php:
    wp_ajax_query_attachments()
  • wp-admin/includes/ajax-actions.php:
    wp_ajax_add_menu_item()
  • wp-admin/includes/meta-boxes.php:
    post_submit_meta_box()
  • wp-admin/includes/class-wp-comments-list-table.php:
    WP_Comments_List_Table::column_response()
  • wp-admin/includes/class-wp-terms-list-table.php:
    WP_Terms_List_Table::column_posts()
  • wp-admin/includes/class-walker-nav-menu-edit.php:
    Walker_Nav_Menu_Edit::start_el()
  • wp-admin/includes/nav-menu.php:
    _wp_ajax_menu_quick_search()
  • wp-admin/includes/nav-menu.php:
    wp_nav_menu_item_post_type_meta_box()
  • 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::no_items()
  • wp-admin/includes/class-wp-posts-list-table.php:
    WP_Posts_List_Table::get_bulk_actions()
  • wp-admin/includes/class-wp-posts-list-table.php:
    WP_Posts_List_Table::extra_tablenav()
  • wp-admin/includes/class-wp-posts-list-table.php:
    WP_Posts_List_Table::__construct()
  • wp-admin/includes/class-wp-posts-list-table.php:
    WP_Posts_List_Table::ajax_user_can()
  • wp-includes/capabilities.php:
    map_meta_cap()
  • wp-includes/class-wp-customize-manager.php:
    WP_Customize_Manager::save()
  • wp-includes/category-template.php:
    wp_list_categories()
  • wp-includes/general-template.php:
    feed_links_extra()
  • wp-includes/general-template.php:
    wp_get_archives()
  • wp-includes/general-template.php:
    wp_title()
  • wp-includes/general-template.php:
    post_type_archive_title()
  • wp-includes/class-wp-query.php:
    WP_Query::get_queried_object()
  • wp-includes/class-wp-query.php:
    WP_Query::is_post_type_archive()
  • wp-includes/class-wp-query.php:
    WP_Query::get_posts()
  • wp-includes/class-wp-query.php:
    WP_Query::parse_query()
  • wp-includes/taxonomy.php:
    register_taxonomy_for_object_type()
  • wp-includes/taxonomy.php:
    unregister_taxonomy_for_object_type()
  • wp-includes/link-template.php:
    wp_get_shortlink()
  • wp-includes/link-template.php:
    get_adjacent_post()
  • wp-includes/link-template.php:
    get_post_type_archive_link()
  • wp-includes/link-template.php:
    get_post_type_archive_feed_link()
  • wp-includes/link-template.php:
    get_edit_post_link()
  • wp-includes/link-template.php:
    get_delete_post_link()
  • wp-includes/link-template.php:
    get_post_permalink()
  • wp-includes/admin-bar.php:
    wp_admin_bar_my_sites_menu()
  • wp-includes/admin-bar.php:
    wp_admin_bar_edit_menu()
  • wp-includes/template.php:
    get_post_type_archive_template()
  • wp-includes/media.php:
    wp_prepare_attachment_for_js()
  • wp-includes/media.php:
    wp_enqueue_media()
  • wp-includes/post.php:
    get_posts_by_author_sql()
  • wp-includes/post.php:
    _count_posts_cache_key()
  • wp-includes/post.php:
    wp_count_posts()
  • wp-includes/post.php:
    _add_post_type_submenus()
  • wp-includes/post.php:
    is_post_type_hierarchical()
  • wp-includes/post.php:
    post_type_exists()
  • wp-includes/canonical.php:
    redirect_canonical()
  • wp-includes/ms-blogs.php:
    _update_blog_date_on_post_publish()
  • wp-includes/ms-blogs.php:
    _update_blog_date_on_post_delete()
  • wp-includes/nav-menu.php:
    wp_setup_nav_menu_item()
  • wp-includes/nav-menu.php:
    wp_update_nav_menu_item()
  • wp-includes/class-wp-xmlrpc-server.php:
    wp_xmlrpc_server::blogger_newPost()
  • wp-includes/class-wp-xmlrpc-server.php:
    wp_xmlrpc_server::mw_newPost()
  • wp-includes/class-wp-xmlrpc-server.php:
    wp_xmlrpc_server::wp_getPostType()
  • wp-includes/class-wp-xmlrpc-server.php:
    wp_xmlrpc_server::wp_getComments()
  • wp-includes/class-wp-xmlrpc-server.php:
    wp_xmlrpc_server::wp_getPosts()
  • wp-includes/class-wp-xmlrpc-server.php:
    wp_xmlrpc_server::_insert_post()
  • wp-includes/class-wp-customize-control.php:
    WP_Customize_Control::render_content()
  • Show 104 more used by
    Hide more used by


  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

    Examples

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

    Technically, this is the same as

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

    print_r( $obj ) might return something like this:

    
    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] => 
    		)
    	[描述] => 
    	[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
    )
    

    Assuming we have the post type ‘certification’, we could do this:

    
    $obj = get_post_type_object( 'certification' );
    

    print_r( $obj ) might return something like this:

    
    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
    		)
    	[描述] => 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
    		)
    )
    

    Note that the object’s attribute names are slightly different than the arguments expected for the register_post_type() function.

让小雨知道,这篇文章帮到了你
扫码关注微信公众号zs40086(微搜片)随时随地微信看片,抢先福利电影等你来

热门推荐

如有疑问,请前往问答中心反馈!

反馈