WP REST Api页面子页面的自定义终结点

时间:2016-03-13 作者:sakarya

我正在尝试为页面子页面添加自定义端点。

父页ID=151

function list_subpages( $data ) {
   $subpages = get_pages( array(
       \'child_of\' => $data[\'151\'],
   ) );

   if ( empty( $subpages ) ) {
       return null;
   }

   return $subpages;
}
add_action(\'rest_api_init\', function () {
    $version = \'2\';
    $namespace = \'wp/v\' . $version;
    $base = \'subpagelist\';
    register_rest_route($namespace, \'/\' . $base, array(
        \'methods\' => \'GET\',
        \'callback\' => array($this, \'list_subpages\'),
    ));
});
获取状态500错误。。

我怎样才能修复它?

1 个回复
最合适的回答,由SO网友:sakarya 整理而成

It works..

function list_subpages() {

   $data = array();
   $request = array();

   $id = 151;
   $subpages = get_pages( array( \'child_of\' => $id, \'sort_column\' => \'menu_order\' ) );

   if ( empty( $subpages ) ) {
       return null;
   }

   foreach ($subpages as $p) {
       $data[\'id\'] = $p->ID;
       $data[\'title\'] = $p->post_title;
       $data[\'img\'] = wp_get_attachment_url( get_post_thumbnail_id($p->ID) );

       $request[] = $data;
   }

   return new WP_REST_Response($request, 200);
}
add_action(\'rest_api_init\', function () {
   $namespace = \'wp/v2\';
   $base = \'hizmetler\';
   register_rest_route($namespace, \'/\' . $base, array(
       \'methods\' => \'GET\',
       \'callback\' => \'list_subpages\',
   ));
});