返回介绍

get_post_types()

发布于 2017-09-10 23:58:08 字数 8126 浏览 1020 评论 0 收藏 0

get_post_types( array|string $args = array(),  string $output = 'names',  string $operator = 'and' )

Get a list of all registered post type objects.


description


参数

$args

(array|string) (Optional) An array of key => value arguments to match against the post type objects.

Default value: array()

$output

(string) (Optional) The type of output to return. Accepts post type 'names' or 'objects'.

Default value: 'names'

$operator

(string) (Optional) The logical operation to perform. 'or' means only one element from the array needs to match; 'and' means all elements must match; 'not' means no elements may match.

Default value: 'and'


返回值

(array) A list of post type names or objects.


源代码

File: wp-includes/post.php

function get_post_types( $args = array(), $output = 'names', $operator = 'and' ) {
	global $wp_post_types;

	$field = ('names' == $output) ? 'name' : false;

	return wp_filter_object_list($wp_post_types, $args, $operator, $field);
}

更新日志

Versiondescription
2.9.0Introduced.

相关函数

Uses

  • wp-includes/functions.php: wp_filter_object_list()

Used By

  • wp-includes/rest-api.php: create_initial_rest_routes()
  • wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php: WP_REST_Users_Controller::get_items()
  • wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php: WP_REST_Users_Controller::get_item_permissions_check()
  • wp-includes/rest-api/endpoints/class-wp-rest-post-statuses-controller.php: WP_REST_Post_Statuses_Controller::check_read_permission()
  • wp-includes/rest-api/endpoints/class-wp-rest-post-statuses-controller.php: WP_REST_Post_Statuses_Controller::get_items_permissions_check()
  • wp-includes/rest-api/endpoints/class-wp-rest-post-types-controller.php: WP_REST_Post_Types_Controller::get_items_permissions_check()
  • wp-includes/rest-api/endpoints/class-wp-rest-post-types-controller.php: WP_REST_Post_Types_Controller::get_items()
  • wp-includes/embed.php: get_post_embed_url()
  • wp-admin/includes/class-wp-screen.php: WP_Screen::render_view_mode()
  • wp-includes/class-wp-customize-nav-menus.php: WP_Customize_Nav_Menus::search_available_items_query()
  • wp-includes/class-wp-customize-nav-menus.php: WP_Customize_Nav_Menus::available_item_types()
  • wp-admin/includes/export.php: export_wp()
  • wp-admin/includes/misc.php: set_screen_options()
  • wp-admin/includes/user.php: wp_delete_user()
  • wp-admin/includes/post.php: wp_edit_posts_query()
  • wp-admin/includes/ajax-actions.php: wp_ajax_menu_get_metabox()
  • wp-admin/includes/ajax-actions.php: wp_ajax_find_posts()
  • wp-admin/includes/class-wp-terms-list-table.php: WP_Terms_List_Table::__construct()
  • wp-admin/includes/nav-menu.php: wp_nav_menu_post_type_meta_boxes()
  • wp-includes/class-wp.php: WP::parse_request()
  • wp-includes/class-wp-query.php: WP_Query::get_posts()
  • wp-includes/class-wp-embed.php: WP_Embed::cache_oembed()
  • wp-includes/link-template.php: get_permalink()
  • wp-includes/link-template.php: get_attachment_link()
  • wp-includes/admin-bar.php: wp_admin_bar_new_content_menu()
  • wp-includes/class-wp-user-query.php: WP_User_Query::prepare_query()
  • wp-includes/post.php: _get_last_post_time()
  • wp-includes/post.php: get_pages()
  • wp-includes/post.php: _add_post_type_submenus()
  • wp-includes/class-wp-rewrite.php: WP_Rewrite::generate_rewrite_rules()
  • wp-includes/rewrite.php: url_to_postid()
  • wp-includes/canonical.php: redirect_guess_404_permalink()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::wp_getPostTypes()
  • wp-includes/class-wp-editor.php: _WP_Editors::wp_link_query()
  • Show 29 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: 1You must log in to vote on the helpfulness of this note Contributed by Codex

    Output a list of only custom post types which are public
    By setting '_builtin' to false, we exclude the WordPress built-in public post types (post, page, attachment, revision, and nav_menu_item) and retrieve only registered custom public post types.

    <?php
    $args = array(
       'public'   => true,
       '_builtin' => false
    );
     
    $output = 'names'; // 'names' or 'objects' (default: 'names')
    $operator = 'and'; // 'and' or 'or' (default: 'and')
     
    $post_types = get_post_types( $args, $output, $operator );
     
    if ( $post_types ) { // If there are any custom public post types.
     
        echo '<ul>';
     
        foreach ( $post_types  as $post_type ) {
            echo '<li>' . $post_type . '</li>';
        }
     
        echo '<ul>';
     
    }
    ?>
  2. Retrieve a named post type as an object
    This example uses the 'object' output to retrieve the post type called ‘movies’ and display its name, singular name and menu icon (an URL):

    <?php
    $args = array(
     	'name' => 'movies',
    );
    
    $post_types = get_post_types( $args, 'objects' );
    
    foreach ( $post_types  as $post_type ) {
       echo '<p>Custom Post Type name: ' . $post_type->name . "<br />\n";
       echo 'Single name: ' . $post_type->labels->singular_name . "<br />\n";
       echo 'Menu icon URL: ' . $post_type->menu_icon . "</p>\n";;
    }
    ?>

    Default Usage
    Retrieves the names (in an array) of all built-in and custom post types.

    <?php $post_types = get_post_types(); ?>

    Output a list of the names of all registered post types
    Uses 'names' as $output argument value.

    <?php
    
    $post_types = get_post_types( '', 'names' ); 
    
    echo '<ul>';
    	
    foreach ( $post_types as $post_type ) {
    
       echo '<li>' . $post_type . '</li>';
    }
    
    echo '</ul>';
    ?>

    Argument values for $args include:

    • public – Boolean. If true, only public post types will be returned.
    • publicly_queryable – Boolean
    • exclude_from_search – Boolean
    • show_ui – Boolean
    • capability_type
    • hierarchical
    • menu_position
    • menu_icon
    • permalink_epmask
    • rewrite
    • query_var
    • _builtin – Boolean. If true, will return WordPress default post types. Use false to return only custom post types.

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

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

发布评论

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