简体   繁体   English

如何通过REST-API获取WordPress中插件的自定义帖子?

[英]How to get custom posts of plugin in WordPress by REST-API?

I am now creating an android app which planned to connect to a plugin(WP Job Manager) in my WordPress side. 我现在正在创建一个计划在WordPress端连接到插件(WP Job Manager)的android应用。 I installed REST-API plugin and successfully getting posts by: 我安装了REST-API插件并成功通过以下方式获取帖子:

mywp.org/wp-json/wp/v2/posts

However, results do not include custom posts which created by the plugin. 但是,结果不包括由插件创建的自定义帖子。 I am new in rest-api and I am not sure what I should do. 我是rest-api的新手,我不确定应该怎么做。

Thankyou! 谢谢!

There are solutions, but the basic idea is that the custom post type must be registered with WP REST API. 有解决方案,但是基本思想是必须使用WP REST API注册自定义帖子类型。

You can refer to this link: http://v2.wp-api.org/extending/custom-content-types/ 您可以参考以下链接: http : //v2.wp-api.org/extending/custom-content-types/

It should be something like this (put it in your functions.php, "cpt" = Custom Post Type, an arbitrary name): 应该是这样的(将其放入您的functions.php中,“ cpt” =自定义帖子类型,任意名称):

function wpsd_add_cpt_args() {
    global $wp_post_types;

    $wp_post_types['cpt']->show_in_rest = true;
    $wp_post_types['cpt']->rest_base = 'cpt';
    $wp_post_types['cpt']->rest_controller_class = 'WP_REST_Posts_Controller';
}
add_action( 'init', 'wpsd_add_cpt_args', 30 );

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

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