简体   繁体   English

wordpress rest api仅通过url获取发布数据

[英]wordpress rest api get post data by url only

is it possible to get post data/type from the wordpress rest api using url as parameter? 是否可以使用url作为参数从wordpress rest api获取发布数据/类型?

I knowing this function already from soundcloud. 我已经从soundcloud知道了这个功能。 There is an resolve function with an url parameter (see https://developers.soundcloud.com/docs/api/reference#resolve ). 有一个带有url参数的resolve函数(请参阅https://developers.soundcloud.com/docs/api/reference#resolve )。 When you call this rest api endpoint, you get detailed information if the url is for example a track or playlist. 当您调用此休息api端点时,如果网址是例如曲目或播放列表,则会获得详细信息。

Unfortunately i couldn't find a function at the wordpress rest api documentation for doing this. 不幸的是,我无法在wordpress rest api文档中找到这样做的功能。 What i exactly want is to get the post data/type by only knowing the url 我真正想要的是通过只知道网址来获取帖子数据/类型

With a function like this https://demo.wp-api.org/wp-json/wp/v2/resolve?url=https://demo.wp-api.org/2017/05/23/hello-world/ 使用这样的函数https://demo.wp-api.org/wp-json/wp/v2/resolve?url=https://demo.wp-api.org/2017/05/23/hello-world /

Is there a way to do this? 有没有办法做到这一点? Or do i need to write my own rest api endpoint with a wordpress plugin? 或者我需要用wordpress插件编写自己的rest api端点吗?

You could just get all the posts https://demo.wp-api.org/wp-json/wp/v2/posts and then do some thing like: 您可以获取所有帖子https://demo.wp-api.org/wp-json/wp/v2/posts然后执行以下操作:

$.each(data, function(index, post){
    if(post.link == yourUrl){
    //yourcode
    }
});

If you don't want to write your own endpoint 如果您不想编写自己的端点

this solved the problem 这解决了这个问题

 function route_handler($request) { global $wp; global $wp_query; $parameters = $request->get_json_params(); $url = $parameters["url"]; $_SERVER['REQUEST_URI'] = $url; $wp->parse_request(); $wp->query_posts(); //Insert queries for more specific information //Modify or simplify the query results //Here I'm just returning the query results. return json_encode($wp_query); } add_action('rest_api_init', function () { register_rest_route('custom-theme/v1', '/route', array( 'methods' => 'POST', 'callback' => 'route_handler', )); }); 

see: https://medium.com/@harryhorton/query-wp-rest-api-using-any-permalink-url-f5e4e4dd36b7 请参阅: https//medium.com/@harryhorton/query-wp-rest-api-using-any-permalink-url-f5e4e4dd36b7

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

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