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

wordpress函数get_post_types()用法示例

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

Get a list of all registered post type objects.


描述


参数

$args

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

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

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);
}

更新日志

Version 描述
2.9.0 Introduced.

相关函数

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


  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.

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

热门推荐

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

反馈