我想使用wordpress的RESTAPI加载模态的内容。我添加了show_in_rest
参数设置为我在函数文件中注册的自定义帖子类型,但我无法加载它,我将收到此错误rest_no_route
. 我怎样才能解决这个问题?
功能:
function staff()
{
$labels = array(
\'name\' => _x( \'Team\', \'post type general name\'),
\'singular_name\' => _x( \'Team\', \'post type singular name\'),
\'menu_name\' => _x( \'Team\', \'admin menu\'),
\'name_admin_bar\' => _x( \'Team\', \'add new on admin bar\'),
\'add_new\' => _x( \'Nuova Persona\', \'Aggiungi membro team\'),
\'add_new_item\' => __( \'Name\'),
\'new_item\' => __( \'Aggiungi Persona\'),
\'edit_item\' => __( \'Modifica Persona\'),
\'view_item\' => __( \'Visualizza Team\'),
\'all_items\' => __( \'Visualizza Tutti\'),
\'featured_image\' => __( \'Featured Image\', \'text_domain\' ),
\'search_items\' => __( \'Cerca Persona\'),
\'parent_item_colon\' => __( \'Parent:\'),
\'not_found\' => __( \'No member found.\'),
\'not_found_in_trash\' => __( \'No member found in Trash.\'),
);
$args = array(
\'labels\' => $labels,
#\'menu_icon\' => \'dashicons-star-half\',
\'description\' => __( \'Description.\'),
\'public\' => true,
\'publicly_queryable\' => true,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'show_in_rest\' => true,
\'query_var\' => true,
\'rewrite\' => true,
\'capability_type\' => \'post\',
\'has_archive\' => true,
\'hierarchical\' => true,
\'menu_position\' => null,
\'supports\' => array(\'title\',\'editor\',\'thumbnail\',\'custom-fields\')
);
register_post_type( \'staff\', $args );
}
add_action(\'init\', \'staff\');
JS/AJAX代码:
$(\'.staff-link\').on(\'click\', function(e){
e.preventDefault();
var id = $(this).attr(\'data-id\');
var type = $(this).attr(\'data-type\');
console.log(id);
$.getJSON(\'https://localhost/wordpress/wp-json/wp/v2/\'+type+\'&id=\'+id, function(data){
console.log(data);
});
});