WordPress中使用json进行分页


Pagination with json in WordPress

我正试图从分页的url中获得一些json输出。它在没有分页的情况下是有效的,但在我添加结束部分时不起作用。

这可能有什么问题?

if ($last_segment == 'getEvents') {
    $vars = explode("/", $_SERVER["REQUEST_URI"]);
    // /events/api/getEvents/page/1/pagesize/10
    //print_r($vars); die;
    if (isset($vars[7])) {
        $posts_per_page = $vars[7];
        $offset = ($vars[5] - 1) * $posts_per_page;
    } else {
        $posts_per_page = -1;
        $offset = 0;
    }
    getEvents($posts_per_page, $offset, isset($vars[9]) ? $vars[9] : NULL );
}

编辑:

print_r($vars); die;

收益率:

Array ( [0] => [1] => events [2] => api [3] => getEvents ) 

使用:/api/getEvents(无分页)

这看起来像是一个典型的一次性错误,php数组从0开始,所以除非explose("/")从request_URI中为您提供一个空的第一个参数。

您能否将REQUEST_URI和$VARS的值转储到您的问题中以帮助调试?

无论如何,我的尝试:

if ($last_segment == 'getEvents') {
    $vars = explode("/", $_SERVER["REQUEST_URI"]);
    // /events/api/getEvents/page/1/pagesize/10
    //print_r($vars); die;
    if (isset($vars[6])) {
        $posts_per_page = $vars[6];
        $offset = ($vars[4] - 1) * $posts_per_page;
    } else {
        $posts_per_page = -1;
        $offset = 0;
    }
    getEvents($posts_per_page, $offset, isset($vars[9]) ? $vars[9] : NULL );
}