自定义发布类型和API REST不起作用

时间:2018-07-25 作者:German Alzate

我创建了一个名为“customers”的自定义帖子类型。我正在按文档所述逐步注册。但当我调用RESTAPI时,它只返回404响应。请参见以下代码:

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(\'customer\', $args);
}
我正在测试的URL是:/wp-json/wp/v2/customers/wp-json/wp/v2/customer

1 个回复
SO网友:Techno Deviser

将此代码置于函数中。php

add_action( \'init\', \'my_customer_cpt\' );
  function my_customer_cpt() {
    $labels = array(
        \'name\'               => _x( \'Customers\', \'post type general name\', \'your-plugin-textdomain\' ),
        \'singular_name\'      => _x( \'Customer\', \'post type singular name\', \'your-plugin-textdomain\' ),
        \'menu_name\'          => _x( \'Customers\', \'admin menu\', \'your-plugin-textdomain\' ),
        \'name_admin_bar\'     => _x( \'Customer\', \'add new on admin bar\', \'your-plugin-textdomain\' ),
        \'add_new\'            => _x( \'Add New\', \'Customer\', \'your-plugin-textdomain\' ),
        \'add_new_item\'       => __( \'Add New Customer\', \'your-plugin-textdomain\' ),
        \'new_item\'           => __( \'New Customer\', \'your-plugin-textdomain\' ),
        \'edit_item\'          => __( \'Edit Customer\', \'your-plugin-textdomain\' ),
        \'view_item\'          => __( \'View Customer\', \'your-plugin-textdomain\' ),
        \'all_items\'          => __( \'All Customers\', \'your-plugin-textdomain\' ),
        \'search_items\'       => __( \'Search Customers\', \'your-plugin-textdomain\' ),
        \'parent_item_colon\'  => __( \'Parent Customers:\', \'your-plugin-textdomain\' ),
        \'not_found\'          => __( \'No customers found.\', \'your-plugin-textdomain\' ),
        \'not_found_in_trash\' => __( \'No customers found in Trash.\', \'your-plugin-textdomain\' )
    );

    $args = array(
        \'labels\'             => $labels,
        \'description\'        => __( \'Description.\', \'your-plugin-textdomain\' ),
        \'public\'             => true,
        \'publicly_queryable\' => true,
        \'show_ui\'            => true,
        \'show_in_menu\'       => true,
        \'query_var\'          => true,
        \'rewrite\'            => array( \'slug\' => \'customer\' ),
        \'capability_type\'    => \'post\',
        \'has_archive\'        => true,
        \'hierarchical\'       => false,
        \'menu_position\'      => null,
        \'show_in_rest\'       => true,
        \'rest_base\'          => \'customers-api\',
        \'rest_controller_class\' => \'WP_REST_Posts_Controller\',
        \'supports\'           => array( \'title\', \'editor\', \'author\', \'thumbnail\', \'excerpt\', \'comments\' )
    );

    register_post_type( \'customer\', $args );
}
然后点击urlhttp://yourdomian.com/wp-json/wp/v2/customers-api

如果结果未显示,则只需点击http://yourdomian.com/wp-json URL然后检查路由并在其“customers api”中搜索rest\\u基名称

确保您在客户帖子类型中有帖子。

结束

相关推荐

Php致命错误:无法将WP_REST_RESPONSE类型的对象用作wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php中

我向WordPress添加了一个自定义端点,如下所示: add_action( \'rest_api_init\', function () { register_rest_route( \'menc/v1\', \'/crosscat/(?P[\\w-]+)/(?P[\\w-]+)\', array( \'methods\' => \'GET\', \'callback\' => \'dept_cat_api\',&#x