简体   繁体   中英

wordpress pagination page links showing 404 error

I am trying to add pagination to product i am getting pagination link eg '1,2 next>>' but when i click on the link it show page not found.

    $args = array( 'post_type' => 'product','product_cat' => 'clothing','posts_per_page' => '1', 'order' => 'DESC','paged' => $paged  );
    
    $loop = new WP_Query( $args );
    if ( $loop->have_posts() ) {
        while ( $loop->have_posts() ) : $loop->the_post();
        echo $product->get_regular_price();
        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 »'),
            ));
        } 
          

  wp_reset_postdata();
}

I think you should change format to:

'format' => '?paged=%#%',

Because current 'format' (if base is correct) gives you for example https://yourwpwebsite.com/basepage/page/1 but it should be https://yourwpwebsite.com/basepage?page=1 so it is still the same page but with page data passed in URL

I am not sure if current 'base' is correct, but you can also try changing it to:

$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
    while ( $loop->have_posts() ) : $loop->the_post();
    echo $product->get_regular_price();
    endwhile;

    $total_pages = $loop->max_num_pages;

    $big = 999999999; // need an unlikely integer

    if ($total_pages > 1){

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

        echo paginate_links(array(
            'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
            'format' => '?paged=%#%',
            'current' => $current_page,
            'total' => $total_pages,
            'prev_text'    => __('« prev'),
            'next_text'    => __('next »'),
        ));
    } 
 wp_reset_postdata();
}
      

Try this, if it is not working, provide more details and current HTML output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM