如何为此自定义的rest API进行分页?

问题描述 投票:0回答:1

我用来从woocommerce获得最畅销的产品并通过REST API返回产品数据

但是我需要建立一个分页系统,以便用户可以定义必须显示的女巫页面。

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的东西例如:

/wp-json/wc/v3/products?per_page=10&page=1
php wordpress-rest-api woocommerce-rest-api
1个回答
0
投票

我正在将代码更改为此:

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”和这两个参数,我可以执行类似分页的操作>

© www.soinside.com 2019 - 2024. All rights reserved.