我如何使用 WP REST API 从自定义帖子类型获取数据

问题描述 投票:0回答:5

我想使用 WP REST API 从我的自定义帖子类型获取数据。

我的自定义帖子类型是“结果”,我已尝试使用此参数。

http://ejobbox.com/wp-json/wp/v2/result/?

http://ejobbox.com/wp-json/wp/v2/result/?per_page=10

还有

http://ejobbox.com/wp-json/wp/v2/posts/?post_type=result

但是我无法使用自定义帖子类型获取数据。

我也将其添加到我的自定义帖子类型中

'show_in_rest'=> true,
'rest_base'          => 'result',
'rest_controller_class' => 'WP_REST_Posts_Controller',

我还是没有得到任何结果。

请告诉我我在这里做错了什么以及我可以做什么来从我的自定义帖子类型获取数据? 请给我一个建议。

我的Function.php(自定义帖子类型)代码在这里:

function codexres_custom_init() {
    $args = array(
      'public' => true,
      'label'  => 'Result'
    );
    register_post_type( 'result', $args );
}
add_action( 'init', 'codexres_custom_init' );


function codex_result_init() {
    $labels = array(
        'name'               => _x( 'Result', 'post type general name', 'your-plugin-textdomain' ),
        'singular_name'      => _x( 'Result', 'post type singular name', 'your-plugin-textdomain' ),
        'menu_name'          => _x( 'Result', 'admin menu', 'your-plugin-textdomain' ),
        'name_admin_bar'     => _x( 'Result', 'add new on admin bar', 'your-plugin-textdomain' ),
        'add_new'            => _x( 'Add New', 'Result', 'your-plugin-textdomain' ),
        'add_new_item'       => __( 'Add New Result', 'your-plugin-textdomain' ),
        'new_item'           => __( 'New Result', 'your-plugin-textdomain' ),

        'edit_item'          => __( 'Edit Result', 'your-plugin-textdomain' ),

        'view_item'          => __( 'View Result', 'your-plugin-textdomain' ),

        'all_items'          => __( 'All Result', 'your-plugin-textdomain' ),

        'search_items'       => __( 'Search Result', 'your-plugin-textdomain' ),

        'parent_item_colon'  => __( 'Parent Result:', 'your-plugin-textdomain' ),
        'not_found'          => __( 'No Result found.', 'your-plugin-textdomain' ),
        'not_found_in_trash' => __( 'No Result 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,
        'show_in_rest'       => true,
        'query_var'          => true,
        'menu_icon'          => 'dashicons-admin-users',
        'rewrite' => array( 'slug' => __('result', 'result')),
        'capability_type'    => 'post',
        'has_archive'        => true,
        'hierarchical'       => false,
        'rest_base'          => 'result',
        'rest_controller_class' => 'WP_REST_Posts_Controller',
        'menu_position'      => 5,
        'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ),
        'taxonomies'       => array('result','category', 'post_tag')

    );

    register_post_type( 'result', $args );
}
wordpress rest
5个回答
11
投票

这是我使用 REST API 从 WordPress 中的自定义帖子类型获取数据的方法。

http://ejobbox.com/wp-json/wp/v2/customposttype
这将返回自定义帖子类型条目的前 10 个结果。


7
投票

如果有人使用自定义帖子类型 UI 插件 - 您需要进入帖子类型设置并通过 UI 执行此操作。

enter image description here


3
投票

你做错了什么:

您有两个功能

codexres_custom_init
codex_result_init
并且这两个功能都注册相同的帖子类型,这不是必需的。尽管对于第二个函数
codex_result_init
,您没有将其添加到
add_action('init','function_name')
。因此,在您的情况下,不需要功能
codexres_custom_init
。要进一步了解自定义帖子类型创建,可以参阅此文档

自定义帖子类型的Rest api

试试这个,我可以看到,你注册了两次结果帖子类型。虽然你没有初始化主要的。

function codex_result_init() {
$labels = array(
    'name'               => _x( 'Result', 'post type general name', 'your-plugin-textdomain' ),
    'singular_name'      => _x( 'Result', 'post type singular name', 'your-plugin-textdomain' ),
    'menu_name'          => _x( 'Result', 'admin menu', 'your-plugin-textdomain' ),
    'name_admin_bar'     => _x( 'Result', 'add new on admin bar', 'your-plugin-textdomain' ),
    'add_new'            => _x( 'Add New', 'Result', 'your-plugin-textdomain' ),
    'add_new_item'       => __( 'Add New Result', 'your-plugin-textdomain' ),
    'new_item'           => __( 'New Result', 'your-plugin-textdomain' ),

    'edit_item'          => __( 'Edit Result', 'your-plugin-textdomain' ),

    'view_item'          => __( 'View Result', 'your-plugin-textdomain' ),

    'all_items'          => __( 'All Result', 'your-plugin-textdomain' ),

    'search_items'       => __( 'Search Result', 'your-plugin-textdomain' ),

    'parent_item_colon'  => __( 'Parent Result:', 'your-plugin-textdomain' ),
    'not_found'          => __( 'No Result found.', 'your-plugin-textdomain' ),
    'not_found_in_trash' => __( 'No Result 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,
    'show_in_rest'       => true,
    'query_var'          => true,
    'menu_icon'          => 'dashicons-admin-users',
    'rewrite' => array( 'slug' => __('result', 'result')),
    'capability_type'    => 'post',
    'has_archive'        => true,
    'hierarchical'       => false,
    'rest_base'          => 'result',
    'rest_controller_class' => 'WP_REST_Posts_Controller',
    'menu_position'      => 5,
    'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ),
    'taxonomies'       => array('result','category', 'post_tag')

);

register_post_type( 'result', $args );
}
  add_action( 'init', 'codex_result_init' );

只需删除以下代码:

function codexres_custom_init() {
    $args = array(
      'public' => true,
      'label'  => 'Result'
    );
    register_post_type( 'result', $args );
}
add_action( 'init', 'codexres_custom_init' );

不要包括这个。


1
投票

用这个

//add this to your functions.php file in your theme folder
function sb_add_cpts_to_api( $args, $post_type ) {
if ( 'result' === $post_type ) {
$args['show_in_rest'] = true;
}
return $args;
}
add_filter( 'register_post_type_args', 'sb_add_cpts_to_api', 10, 2 );

我从这里

得到的

它像巫毒一样工作


0
投票

尝试使用这个 'show_in_rest' => 真, 在 $args 中

© www.soinside.com 2019 - 2024. All rights reserved.