Php 如何对此自定义rest API进行分页?

Php 如何对此自定义rest API进行分页?,php,wordpress-rest-api,woocommerce-rest-api,Php,Wordpress Rest Api,Woocommerce Rest Api,我使用它从woocommerce获取最畅销的产品,并在RESTAPI中返回产品数据 但我需要做一个分页系统,以便用户可以定义必须显示的页面 function featureproduct(){ $prdtc=wc_get_featured_product_ids(); $array=array(); $i=0; foreach ($prdtc as $id) { $res = wc_get_product( $id); $array[$i]=arr

我使用它从woocommerce获取最畅销的产品,并在RESTAPI中返回产品数据

但我需要做一个分页系统,以便用户可以定义必须显示的页面

function featureproduct(){
$prdtc=wc_get_featured_product_ids();
$array=array();
$i=0;
    foreach ($prdtc as $id)
    {
        $res = wc_get_product( $id);
        $array[$i]=array(
$id,
 $res->get_name(),
$res->get_price(),
get_the_post_thumbnail_url($id),
get_permalink($id)
);
    $i++;
}
    echo json_encode($array,JSON_FORCE_OBJECT);
}
我需要像你用的那样的东西 例如:

/wp-json/wc/v3/products?per_page=10&page=1

我正在将代码更改为:

function featureproduct($tedad1){
    $tedad=$tedad1['tedad'];
    $offset=$tedad1['offset'];
global $wpdb;
$query="
SELECT id FROM `wp_posts` WHERE post_type='product' 
AND post_status='pending' 
ORDER BY post_date ASC LIMIT ".$offset.", ".$tedad."";

$prdtc=$wpdb->get_results($query);
$array=array();
$i=0;
foreach ($prdtc as $id)
{
    $res = wc_get_product( $id->id);
    $array[$i]=array(
$id->id, $res->get_name(),
$res->get_price(),
get_the_post_thumbnail_url($id->id),
get_permalink($id->id));

    $i++;
}
    echo json_encode($array,JSON_FORCE_OBJECT);

}
有了“LIMIT”和这两个参数,我可以做一些类似分页的事情