【问题标题】:Custom API Wordpress Endpoint fails自定义 API Wordpress 端点失败
【发布时间】:2018-09-14 08:14:54
【问题描述】:

{"code":"rest_no_route","message":"没有找到匹配 URL 和请求方法的路由","data":{"status":404}}

我试图确定我的代码有什么问题。我已经为 wordpress 编写了一个自定义 api 端点,但是在尝试访问 url 时出现上述错误。我的代码如下:

 $this->loader->add_action('rest_api_init', $plugin_public, 'create_api_webhook');

 public function create_api_webhook() {
    register_rest_route('learn2/v1', '/api/', array(
        'methods' => 'GET',
        'callback' => array($this, 'learn2_api_webhook')
    ));
}

public function learn2_api_webhook() {
    global $wpdb;
    $args = array('post_type' => 'sfwd-courses',
        'post_status' => 'publish',
        'order' => 'DESC',
        'orderby' => 'ID',
    );
    $loop = new WP_Query($args);
    $courses = array();
    $lessons = array();
    while ($loop->have_posts()) : $loop->the_post();

        $mylessons = learndash_get_lesson_list($post->ID);
        $lessons = array();
        foreach ($mylessons as $lesson) {
            $topics = array();
            $mytopics = learndash_get_topic_list($lesson->ID, $post->ID);
            foreach ($mytopics as $topic) {
                $topics[] = array('id' => $topic->ID, 'title' => $topic->post_title);
            }
            $lessons[] = array('id' => $lesson->ID, 'title' => $lesson->post_title, 'topics' => $topics);
        }
        $courses[] = array('id' => $post->ID, 'title' => $post->post_title, 'lessons' => $lessons);


    endwhile;


    return json_encode($courses);
}

我正在尝试通过以下方式访问网址:

http://localhost/community_staging/wp-json/learn2/v1/api/ but I get the above error.

我正在运行最新的 wordpress

【问题讨论】:

    标签: wordpress wordpress-rest-api


    【解决方案1】:
    $this->loader->add_action('rest_api_init', $plugin_public, 'create_api_webhook');
    

    $plugin_public 没有指向正确的类。

    【讨论】:

      猜你喜欢
      • 2020-08-24
      • 1970-01-01
      • 2020-07-23
      • 1970-01-01
      • 1970-01-01
      • 2017-04-02
      • 1970-01-01
      • 2018-05-14
      • 2020-09-01
      相关资源
      最近更新 更多