获取使用WP REST API自定义信息(Get custom posts using the WP

2019-10-28 19:50发布

我试图让自定义后的类型,但我似乎无法找到任何解决方案。 该WP REST API文档只返回博客文章。

注:我使用的Dooplay主题

下面的代码是在文件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'); } 

Answer 1:

与之比较

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访问。

附录

其实只有“show_in_rest”需要为WordPress的具有另外两个默认值。 此外,如果开发商写了他自己WP_REST_Controller自定义职位类型,而不是使用内置的WP_REST_Posts_Controller那么即使不是必需为自己WP_REST_Controller可以通过其他方式调用register_rest_route()函数。



文章来源: Get custom posts using the WP REST API