【问题标题】:Getting all Posts Wordpress in Json with URL使用 URL 获取 Json 中的所有帖子 Wordpress
【发布时间】:2017-04-11 17:00:24
【问题描述】:
我使用链接:http://MYWEBSITE/wp-json/wp/v2/posts 来查看我的 Wordpress 的所有帖子。
但实际上它并没有显示全部。它只显示数据库中“post-type”等于“post”的帖子。我还有其他人的“帖子类型”为“博客”。
我找不到可以为我提供这些博客文章的 JSON 的 URL。
所以如果你有什么想法告诉我!
这是一个 Messenger 机器人,它将获取所有帖子,以便在用户请求时显示它们。
我没有设法在 Glitch.com 中使用 PHP,所以如果你对此也有想法.. :D
谢谢。
【问题讨论】:
标签:
json
wordpress
bots
posts
messenger
【解决方案1】:
您应该公开您的自定义帖子类型。供参考使用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';
}
}
然后就可以通过http://MYWEBSITE/wp-json/wp/v2/blog访问了