du16178 2018-07-25 20:21
浏览 44

自定义帖子类型和API REST无法正常工作

I have been created a custom post type called 'customers'. I'm registering step by step as documentation says. But when I call to rest api, It just return a 404 response. See the code below:

add_action('init', 'customer_post_type');
function customer_post_type() {
    $labels = array(
        'name'                => _x('Customers', 'customers'),
        'singular_name'       => _x('Customer', 'customers'),
        'menu_name'           => __('Customers', 'customers'),
        'parent_item_colon'   => __('Parent Customer', 'customers'),
        'all_items'           => __('All Customers', 'customers'),
        'view_item'           => __('View Customer', 'customers'),
        'add_new_item'        => __('Add New Customer', 'customers'),
        'add_new'             => __('Add New', 'customers'),
        'edit_item'           => __('Edit Customer', 'customers'),
        'update_item'         => __('Update Customer', 'customers'),
        'search_items'        => __('Search Customer', 'customers'),
        'not_found'           => __('Not Found', 'customers'),
        'not_found_in_trash'  => __('Not found in Trash', 'customers'),
    );

    $args = array(
        'label'               => __('Customers', 'customers'),
        'description'         => __('Customer news and reviews', 'customers'),
        'labels'              => $labels,
        'supports'            => array('title', 'author', 'thumbnail', 'custom-fields', ),
        'hierarchical'        => false,
        'public'              => false,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'has_archive'         => false,
        'exclude_from_search' => true,
        'publicly_queryable'  => false,
        'menu_icon'           => 'dashicons-desktop',
        'show_in_rest'        => true,
        'rest_base'           => 'customer',
        'rest_controller_class' => 'WP_REST_Post_Controller'
    );

    register_post_type('customers', $args);
}

the urls that I testing for, are: /wp-json/wp/v2/customers and /wp-json/wp/v2/customer

  • 写回答

1条回答 默认 最新

  • dsfsw1233 2018-07-26 06:33
    关注

    Something I had to do, was instead of making it "restable" when i register the post type, I did it later.

    like this.

    add_action( 'init', 'my_custom_post_type_rest_support', 25 );
    
    function my_custom_post_type_rest_support() {
        global $wp_post_types;
    
        //be sure to set this to the name of your post type!
        $post_type_name_customers = 'customer';
        if( isset( $wp_post_types[ $post_type_name_customers ] ) ) {
            $wp_post_types[$post_type_name_customers]->show_in_rest = true;
            $wp_post_types[$post_type_name_customers]->rest_base = $post_type_name_customers;
            $wp_post_types[$post_type_name_customers]->rest_controller_class = 'WP_REST_Posts_Controller';
        }
    }
    

    Like this, I was able to get my CPT through rest.

    You could try it out -

    Remember to remove them from the registration of the post form.

    Also, you might have to set "public" => true But i am not sure.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看