使用 WP REST API 获取自定义帖子 [英] Get custom posts using the WP REST API

查看:35
本文介绍了使用 WP REST API 获取自定义帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取自定义帖子类型,但似乎找不到任何解决方案.WP REST API 文档仅返回博客文章.

I'm trying to get custom post types, but I can not seem to find any solution. The WP REST API documentation only returns blog posts.

注意:我使用的是 Dooplay 主题

Note: I'm using the Dooplay theme

下面的代码在文件tipo.php

目录:inc > 包含 > 系列

if( ! function_exists( 'doo_series' ) ) {
    function doo_series() {
        $labels = array(
            'name'                => _x('TV Shows', 'Post Type General Name','mtms'),
            'singular_name'       => _x('TV Show', 'Post Type Singular Name','mtms'),
            'menu_name'           => __d('TV Shows %%PENDING_COUNT_TV%%'),
            'name_admin_bar'      => __d('TV Shows'),
            'all_items'           => __d('TV Shows'),
        );
        $rewrite = array(
            'slug'                => get_option('dt_tvshows_slug','tvshows'),
            'with_front'          => true,
            'pages'               => true,
            'feeds'               => true,
        );
        $args = array(
            'label'               => __d('TV Show'),
            'description'         => __d('TV series manage'),
            'labels'              => $labels,
            'supports'            => array('title', 'editor','comments','thumbnail','author'),
            'taxonomies'          => array('genres'),
            'hierarchical'        => false,
            'public'              => true,
            'show_ui'             => true,
            'show_in_menu'        => true,
            'menu_position'       => 5,
            'menu_icon'           => 'dashicons-welcome-view-site',
            'show_in_admin_bar'   => true,
            'show_in_nav_menus'   => false,
            'can_export'          => true,
            'has_archive'         => true,
            'exclude_from_search' => false,
            'publicly_queryable'  => true,
            'rewrite'             => $rewrite,
            'capability_type'     => 'post',
        );
        register_post_type('tvshows', $args );
    }
    add_action('init', 'doo_series', 0 );
    get_template_part('inc/includes/series/metabox');
}

推荐答案

比较

register_post_type( 'post', array(
    'labels' => array(
        'name_admin_bar' => _x( 'Post', 'add new from admin bar' ),
    ),
    'public'  => true,
    '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
    '_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */
    'capability_type' => 'post',
    'map_meta_cap' => true,
    'menu_position' => 5,
    'hierarchical' => false,
    'rewrite' => false,
    'query_var' => false,
    'delete_with_user' => true,
    'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'post-formats' ),
    'show_in_rest' => true,
    'rest_base' => 'posts',
    'rest_controller_class' => 'WP_REST_Posts_Controller',
) );

注册内置的帖子类型帖子.观察最后三个字段.必须设置这些字段才能通过 REST 访问帖子类型.

which registers the built in post type post. Observe the last three fields. These fields must be set to make the post type accessible via REST.

附录

实际上只有 'show_in_rest' 是必需的,因为 WordPress 有其他两个的默认值.此外,如果开发人员为自定义帖子类型编写了自己的 WP_REST_Controller 而不是使用内置的 WP_REST_Posts_Controller,那么即使这也不是必需的,因为他自己的 WP_REST_Controller 可以通过其他方式调用 register_rest_route() 函数.

Actually only 'show_in_rest' is required as WordPress has default values for the other two. Also, if a developer wrote his own WP_REST_Controller for the custom post type instead of using the builtin WP_REST_Posts_Controller then even this is not required as his own WP_REST_Controller can call the register_rest_route() functions by some other means.

这篇关于使用 WP REST API 获取自定义帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆