WordPress获取所有文章类型函数:get_post_types

2022/07/13

WordPress函数get_post_types用于获取所有文章类型信息。

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

本文涉及的内容包括:

函数参数

$args

数组或字符串

根据传递的参数返回符合条件的文章类型,以下示例只返回post:

get_post_types(array('name'=>'post'), 'objects')

$output

字符串,默认值:names

可用值:

  • names:返回的信息比较简洁;
  • objects:返回详细的文章类型数据。

$operator

字符串,默认值:and

要执行的逻辑操作

函数返回值

当$output的值为names,返回信息如下:

Array
(
    [post] => post
    [page] => page
    [attachment] => attachment
    [revision] => revision
    [nav_menu_item] => nav_menu_item
    [custom_css] => custom_css
    [customize_changeset] => customize_changeset
    [oembed_cache] => oembed_cache
    [user_request] => user_request
    [wp_block] => wp_block
)

当$output的值为objects,返回信息如下:

Array
(
    [post] => WP_Post_Type Object
        (
            [name] => post
            [label] => 文章
            [labels] => stdClass Object
                (
                    [name] => 文章
                    [singular_name] => 文章
                    [add_new] => 写文章
                    [add_new_item] => 撰写新文章
                    [edit_item] => 编辑文章
                    [new_item] => 写文章
                    [view_item] => 查看文章
                    [view_items] => 查看文章
                    [search_items] => 搜索文章
                    [not_found] => 未找到文章。
                    [not_found_in_trash] => 回收站中没有文章。
                    [parent_item_colon] => 
                    [all_items] => 所有文章
                    [archives] => 文章归档
                    [attributes] => 文章属性
                    [insert_into_item] => 插入至文章
                    [uploaded_to_this_item] => 上传到本文章的
                    [featured_image] => 特色图像
                    [set_featured_image] => 设置特色图像
                    [remove_featured_image] => 取消特色图像
                    [use_featured_image] => 设置为特色图像
                    [filter_items_list] => 过滤文章列表
                    [items_list_navigation] => 文章列表导航
                    [items_list] => 文章列表
                    [item_published] => 文章已发布。
                    [item_published_privately] => 文章已私密发布。
                    [item_reverted_to_draft] => 文章已恢复为草稿。
                    [item_scheduled] => 文章已排入发布计划。
                    [item_updated] => 文章已更新。
                    [menu_name] => 文章
                    [name_admin_bar] => 文章
                )

            [description] => 
            [public] => 1
            [hierarchical] => 
            [exclude_from_search] => 
            [publicly_queryable] => 1
            [show_ui] => 1
            [show_in_menu] => 1
            [show_in_nav_menus] => 1
            [show_in_admin_bar] => 1
            [menu_position] => 5
            [menu_icon] => dashicons-admin-post
            [capability_type] => post
            [map_meta_cap] => 1
            [register_meta_box_cb] => 
            [taxonomies] => Array
                (
                )

            [has_archive] => 
            [query_var] => 
            [can_export] => 1
            [delete_with_user] => 1
            [_builtin] => 1
            [_edit_link] => post.php?post=%d
            [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
                    [delete_posts] => delete_posts
                    [publish_posts] => publish_posts
                    [read_private_posts] => read_private_posts
                    [read] => read
                    [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
                )

            [rewrite] => 
            [show_in_rest] => 1
            [rest_base] => posts
            [rest_controller_class] => WP_REST_Posts_Controller
            [rest_controller] => 
        )

    ......
)

函数使用示例

$args = array(
	'public'   => true,
	'_builtin' => false
);

$output = 'names';
$operator = 'and';
$post_types = get_post_types( $args, $output, $operator );

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注