当前位置:首页>网站建设>WordPress创建post type自定义文章类型时添加rest api支持

WordPress创建post type自定义文章类型时添加rest api支持

释放双眼,带上耳机,听听看~!

要在WordPress中为自定义文章类型添加REST API支持,您需要使用register_post_type()函数时设置show_in_rest参数为true。以下是一些示例代码,说明如何创建一个自定义文章类型并为其添加REST API支持:

function custom_post_type() {
    $labels = array(
        'name'               => 'Custom Posts',
        'singular_name'      => 'Custom Post',
        'add_new'            => 'Add New',
        'add_new_item'       => 'Add New Custom Post',
        'edit_item'          => 'Edit Custom Post',
        'new_item'           => 'New Custom Post',
        'all_items'          => 'All Custom Posts',
        'view_item'          => 'View Custom Post',
        'search_items'       => 'Search Custom Posts',
        'not_found'          => 'No Custom Posts found',
        'not_found_in_trash' => 'No Custom Posts found in Trash',
        'menu_name'          => 'Custom Posts'
    );
 
    $args = array(
        'labels'             => $labels,
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'query_var'          => true,
        'rewrite'            => array( 'slug' => 'custompost' ),
        'capability_type'    => 'post',
        'has_archive'        => true,
        'hierarchical'       => false,
        'menu_position'      => null,
        'supports'           => array( 'title', 'editor', 'thumbnail', 'excerpt' ),
        'show_in_rest'       => true, // 启用 REST API 支持
    );
 
    register_post_type( 'custom_post', $args );
}
 
add_action( 'init', 'custom_post_type' );

上述代码创建了一个名为“Custom Posts”的自定义文章类型,并启用了REST API支持。您可以根据需要调整参数,以满足您的项目要求。

欢迎访问秀主题博客,分享简单实用WP教程

一旦您启用了REST API支持,您就可以使用REST API来访问和操作自定义文章类型的内容。默认情况下,您可以在/wpjson/wp/v2/custom_post端点下找到自定义文章类型的REST API。例如,要获取自定义文章类型的所有文章,您可以访问/wpjson/wp/v2/custom_post。

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

给TA打赏
共{{data.count}}人
人已打赏
网站建设

WordPress获取文章统计数量及评论数量

2025-3-16 10:10:46

电脑系统

微软承认 OneDrive 共享文件夹变 .url 文件问题:已存在近 10 个月,修复时间尚不确定

2025-3-8 13:48:37

温馨提示:

1.本站大部分内容均收集于网络!若内容若侵犯到您的权益,请发送邮件至:971318156@qq.com,我们将第一时间处理!

2.资源所需价格并非资源售卖价格,是收集、整理、编辑详情以及本站运营的适当补贴,并且本站不提供任何免费技术支持。

3.所有资源仅限于参考和学习,版权归原作者所有,更多请阅读网站声明

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索