我想使用 WP REST API 从自定义帖子类型获取数据。
我的自定义帖子类型是“结果”,我已尝试使用此参数。
http://ejobbox.com/wp-json/wp/v2/result/?
http://ejobbox.com/wp-json/wp/v2/result/?per_page=10
Run Code Online (Sandbox Code Playgroud)
并且还与
http://ejobbox.com/wp-json/wp/v2/posts/?post_type=result
Run Code Online (Sandbox Code Playgroud)
但我无法使用自定义帖子类型获取数据。
我还将其添加到我的自定义帖子类型中
'show_in_rest'=> true,
'rest_base' => 'result',
'rest_controller_class' => 'WP_REST_Posts_Controller',
Run Code Online (Sandbox Code Playgroud)
我仍然没有得到任何结果。
请问,您能告诉我我在这里做错了什么吗?我该怎么做才能从自定义帖子类型中获取数据?请给我一个建议。
我的 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 );
}
Run Code Online (Sandbox Code Playgroud)
Kar*_*N G 10
这就是我使用 REST API 从 WordPress 中的自定义帖子类型获取数据的方法。
http://ejobbox.com/wp-json/wp/v2/customposttype这将返回自定义帖子类型条目的前 10 个结果。
你做错了什么:
您有两个函数codexres_custom_init,codex_result_init并且两个函数都注册相同的帖子类型,这不是必需的。尽管对于第二个函数,codex_result_init您没有将其添加到add_action('init','function_name'). 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 );
}
add_action( 'init', 'codex_result_init' );
Run Code Online (Sandbox Code Playgroud)
只需删除下面的代码:
function codexres_custom_init() {
$args = array(
'public' => true,
'label' => 'Result'
);
register_post_type( 'result', $args );
}
add_action( 'init', 'codexres_custom_init' );
Run Code Online (Sandbox Code Playgroud)
不要包括这个呀。
| 归档时间: |
|
| 查看次数: |
38896 次 |
| 最近记录: |