返回介绍

get_post_type()

发布于 2017-09-10 23:57:51 字数 4879 浏览 1067 评论 0 收藏 0

get_post_type( int|WP_Post|null $post = null )

Retrieves the post type of the current post or of a given post.


description


参数

$post

(int|WP_Post|null) (Optional) Post ID or post object. Default is global $post.

Default value: null


返回值

(string|false) Post type on success, false on failure.


源代码

File: wp-includes/post.php

function get_post_type( $post = null ) {
	if ( $post = get_post( $post ) )
		return $post->post_type;

	return false;
}

更新日志

Versiondescription
2.1.0Introduced.

相关函数

Uses

  • wp-includes/post.php: get_post()

Used By

  • wp-includes/widgets/class-wp-widget-media.php: WP_Widget_Media::has_content()
  • wp-includes/class-wp-customize-manager.php: WP_Customize_Manager::has_published_pages()
  • wp-includes/class-wp-customize-manager.php: WP_Customize_Manager::find_changeset_post_id()
  • wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php: WP_REST_Attachments_Controller::create_item()
  • wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php: WP_REST_Attachments_Controller::update_item()
  • wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php: WP_REST_Posts_Controller::prepare_item_for_database()
  • wp-includes/class-wp-customize-nav-menus.php: WP_Customize_Nav_Menus::save_nav_menus_created_posts()
  • wp-includes/embed.php: get_oembed_response_data_rich()
  • wp-admin/includes/media.php: get_media_item()
  • wp-admin/includes/post.php: bulk_edit_posts()
  • wp-admin/includes/nav-menu.php: _wp_ajax_menu_quick_search()
  • wp-includes/capabilities.php: map_meta_cap()
  • wp-includes/category-template.php: get_the_category_list()
  • wp-includes/class-wp-theme.php: WP_Theme::get_page_templates()
  • wp-includes/post.php: wp_check_for_changed_slugs()
  • wp-includes/post.php: wp_set_post_categories()
  • wp-includes/nav-menu.php: is_nav_menu_item()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::_insert_post()
  • Show 13 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: 3You must log in to vote on the helpfulness of this note Contributed by simonrcodrington

    Default WordPress Post Type Names
    Here are the default post type names. Use these as the parameter for this function to collect the post type object

    • post
    • page
    • attachment
    • revision
    • nav_menu_item
  2. Display the post type. This example needs to be inside the loop.

    
    printf( __( 'The post type is: %s', 'textdomain' ), get_post_type( get_the_ID() ) );
    

    Conditional sentence to see if it is a post type.

    
    if ( get_post_type( get_the_ID() ) == 'slug_post_type' ) {
        //if is true
    }
    

    get_post_type();
    is commonly used in conjunction with Post Formats.
    Functionality is extended in themes by including:
    add_theme_support( 'post-formats', array( 'aside', 'gallery' ) );
    in your functions.php file.

    After including this in your theme’s functions.php file, the option to choose a post type (that you included inside the array) will appear in the right sidebar when creating/editing a post.

    If no Post Type is selected, WordPress selects the default which is Standard.

    WordPress supported post formats (that are not defaults) are:
    'aside', 'gallery', 'link', 'image', 'quote', 'status', 'video', 'audio', 'chat'

    Everything you need to know about using & styling Post Formats is here: https://codex.wordpress.org/Post_Formats

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

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

发布评论

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