Woocommerce custom products list pagination

前端 未结 2 1367
無奈伤痛
無奈伤痛 2021-01-23 16:23

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

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

$loo         


        
2条回答
  •  不思量自难忘°
    2021-01-23 16:58

    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();
    

提交回复
热议问题