Woocommerce 自定义产品列表分页

问题描述:

我有这样的代码用于在 woocommerce 中获取和显示产品:

I have code like this for fetching and displaying products in woocommerce:

{
$args = array(
'post_type' => 'product',
'posts_per_page' => 30
);

$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
//display results here;
endwhile;
}

问题是当代码按照指定一次显示 30 个产品时,它没有添加页面链接以便可以查看其他产品,有没有办法做到这一点,或者我错过了什么?

The problem is while the code displays 30 products at a time as specified, it does not add page links so other products could be viewed, is there a way to do this or am I missing somethin?

你能看看下面的代码吗?我希望这段代码对你有用.

Can you please check below code? i hope this code is work for you.

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

$args = array(
    'post_type'=>'product',
    'posts_per_page' => 30,
    'paged' => $paged,
);

$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
    while ( $loop->have_posts() ) : $loop->the_post();
        //display results here;
    endwhile;

    $total_pages = $loop->max_num_pages;

    if ($total_pages > 1){

        $current_page = max(1, get_query_var('paged'));

        echo paginate_links(array(
            'base' => get_pagenum_link(1) . '%_%',
            'format' => '/page/%#%',
            'current' => $current_page,
            'total' => $total_pages,
            'prev_text'    => __('« prev'),
            'next_text'    => __('next »'),
        ));
    }    
endif;
wp_reset_postdata();