简体   繁体   English

Wordpress REST API端点

[英]Wordpress REST API Endpoint

I am writing an Angular WP theme and i'm trying to reduce the number of HTTP requests on the post page. 我正在写一个Angular WP主题,我正在尝试减少帖子页面上的HTTP请求数量。

On the post page I want to list all the different taxonomies, recent posts, get the featured image and a few other things. 在帖子页面上,我想列出所有不同的分类,最近的帖子,获取特色图片和其他一些东西。 I can do this all with individual requests with the REST API v2 plugin but that's a lot of requests. 我可以使用REST API v2插件执行所有这些操作,但这是很多请求。

I was hoping to create an endpoint for my theme, parse the post slug and get it all back in one request but I can't seem to figure it out. 我希望为我的主题创建一个端点,解析post slug并在一个请求中将其全部恢复,但我似乎无法弄明白。

I was thinking of using query string to get the slug. 我正在考虑使用查询字符串来获取slug。 Here's what I have been using to test it out: 以下是我用来测试它的内容:

function app_get_post($data) {
    global $wp_query;

    return [
        'test' => $data,
        'vars' => $wp_query->query_vars
    ];
}

add_action( 'rest_api_init', function () {
    register_rest_route( 'app/v1', '/post', [
        'methods' => 'GET',
        'callback' => 'app_get_post',
    ] );
} );

Here's what it produces: 这是它产生的东西:

{
test: { },
vars: [ ]
}

I did try adding the query var with a query_vars hook but it didn't work either. 我尝试使用query_vars钩子添加查询var,但它也不起作用。

Any suggestions? 有什么建议? Am I going about this the right way? 我是以正确的方式来做这件事的吗?

you should pass the parameter 你应该传递参数

function app_get_post($data) {

    return [
        'test' => $data["postid"]        
    ];
}

add_action( 'rest_api_init', function () {
    register_rest_route( 'app/v1', '/post/(?P<postid>\d+)', [
        'methods' => 'GET',
        'callback' => 'app_get_post',
    ] );
} )

refer http://wiki.workassis.com/wordpress-create-rest-api/ for example 例如,请参阅http://wiki.workassis.com/wordpress-create-rest-api/

So I'm learning about all this as well and haven't met success yet but will offer my research points. 所以我也在了解这一切,但尚未取得成功,但会提供我的研究方向。 I know that to tap into a Custom Post Type you would need to add show_in_rest=true to the register_post_type() array or hook into it later. 我知道要进入自定义帖子类型,您需要将show_in_rest=true添加到register_post_type()数组或稍后挂钩。

However your example shows the use of post which I'm guessing should have show_in_rest where it is registered... I decided to check in wp-includes/post.php and it does not exist. 然而你的例子显示了我猜的应该有show_in_rest注册的帖子的使用...我决定检查wp-includes / post.php并且它不存在。

// ACCESSING A CPT FROM ANYWHERE (unable to add show_in_rest=true to the register_post_type() hook into it!)
// http://scottbolinger.com/custom-post-types-wp-api-v2/
function sb_add_cpts_to_api( $args, $post_type ) {
    if ( 'movie' === $post_type ) {
        $args['show_in_rest'] = true;
        $args['rest_base'] = 'movie';
        // $args['rest_controller_class'] = 'WP_REST_Posts_Controller';
    }
    return $args;
}
add_filter( 'register_post_type_args', 'sb_add_cpts_to_api', 10, 2 );

Here you need to pass Parameter as show below 在这里你需要传递参数如下所示

//Endpoint : http://wpdadd.com/wp-json/api/v1/getwp/ {parameter 1}/{parameter2} //端点: http//wpdadd.com/wp-json/api/v1/getwp/ {parameter 1} / {parameter2}

register_rest_route( 'api', '/v1/getwp/(?P<param1>[a-z0-9\-]+)/(?P<param2>[a-z0-9\-]+)', array(
    'methods' => 'GET',
    'callback' => 'wpdaddStuff'

));

for more information you can check this example here http://wpdadd.com/create-custom-rest-api-wordpress/ 有关更多信息,请查看此示例http://wpdadd.com/create-custom-rest-api-wordpress/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM