简体   繁体   English

使用URL获取Json中所有Wordpress的帖子

[英]Getting all Posts Wordpress in Json with URL

I use the link : http://MYWEBSITE/wp-json/wp/v2/posts to see all the posts of my Wordpress. 我使用链接: http:// MYWEBSITE / wp-json / wp / v2 / posts来查看我的Wordpress的所有帖子。 But in fact it doesn't show ALL. 但实际上,它并不显示全部。 It only shows the posts with the "post-type" equals to "post" in the data base. 它仅显示数据库中“ post-type”等于“ post”的帖子。 I have others with the "post-type" as "blog". 我还有其他人以“帖子类型”为“博客”。

I can't find where I can have an URL that will give me the JSON of these blog posts. 我找不到在哪里可以提供这些博客文章的JSON的URL。 So if you have any idea tell me ! 所以,如果您有任何想法告诉我!

It's for a Messenger bot, that will get all posts so as to show them when the user ask for it. 它是针对Messenger机器人的,它将获取所有帖子,以便在用户要求时显示它们。 I didn't managed to use PHP in Glitch.com, so if you have an idea on this too.. :D 我没有在Glitch.com上使用PHP,所以如果您对此也有想法。.:D

Thank you. 谢谢。

You should expose your custom post type. 您应该公开您的自定义帖子类型。 For the reference use documentation . 有关参考,请使用文档

 /**
  * Add REST API support to an already registered post type.
  */
  add_action( 'init', 'my_custom_post_type_rest_support', 25 );
  function my_custom_post_type_rest_support() {
    global $wp_post_types;

    //be sure to set this to the name of your post type!
    $post_type_name = 'blog';
    if( isset( $wp_post_types[ $post_type_name ] ) ) {
        $wp_post_types[$post_type_name]->show_in_rest = true;
        $wp_post_types[$post_type_name]->rest_base = $post_type_name;
        $wp_post_types[$post_type_name]->rest_controller_class = 'WP_REST_Posts_Controller';
    }

  }

Then you can access it trough http://MYWEBSITE/wp-json/wp/v2/blog 然后您可以通过http://MYWEBSITE/wp-json/wp/v2/blog访问它

Thank you for your answer. 谢谢您的回答。 I read the Doc, but the problem now is that it's in PHP. 我阅读了Doc,但是现在的问题是它在PHP中。 As I said I didn't managed to use PHP in Glitch.com, is it possible ? 正如我说的那样,我没有在Glitch.com中使用PHP,这可能吗? It could solve the problem. 它可以解决问题。

At the same time I started using : https://www.npmjs.com/package/wordpress-rest-api . 同时,我开始使用: https : //www.npmjs.com/package/wordpress-rest-api Allowed me to get the Posts only once again. 只允许我再次获得该帖子。 I tried to do as they did to "discover" my website, but it seems that it can't find the route, maybe because it doesn't already exist. 我试图像他们一样“发现”我的网站,但是似乎找不到路线,可能是因为它尚不存在。 So I'm stuck once again, if you have time to check this doc and se if you have an idea on what to do, I would appreciate. 因此,我再次陷入困境,如果您有时间检查此文档,并且对如何做有任何想法,我将不胜感激。

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

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