Habib
Habib

Reputation: 308

Page not found Error on navigating

I Searched internet but couldn't found the solution. I am getting 404 error when I navigate to any other page using wp pagenavi,

 <ul class="product-items">
        <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

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

    $product_query = new WP_Query($args);

    if($product_query->have_posts()) : while($product_query ->have_posts()) : $product_query ->the_post(); 
     $id = get_the_ID();

    ?>


            <li>
                    <a href="<?php the_permalink(); ?>">
                    <span class="product-img"><?php echo get_the_post_thumbnail($id, array(101,128,true)) ?></span>
                    <span class="product-detail"><?php $title=get_the_title(); echo $trimed=wp_trim_words($title,3) ?></span>
            </a>
                </li>
                <?php endwhile;   if(function_exists('wp_pagenavi')) { wp_pagenavi( array( 'query' => $product_query)); }

wp_reset_postdata(); ?>

Upvotes: 0

Views: 92

Answers (1)

vard
vard

Reputation: 4136

That's an really edge case where using query_posts can be usefull, as you need to replace the main query in order to make your pagination plugin to work. So you'll have to change your query to this:

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

$product_query = new WP_Query($args);

if(have_posts()):
    while(have_posts()) : the_post(); 
        $id = get_the_ID();
        ?>
        <li>
                <a href="<?php the_permalink(); ?>">
                <span class="product-img"><?php echo get_the_post_thumbnail($id, array(101,128,true)) ?></span>
                <span class="product-detail"><?php $title=get_the_title(); echo $trimed=wp_trim_words($title,3) ?></span></a>
        </li>
    <?php endwhile;  
     if(function_exists('wp_pagenavi')) { 
        wp_pagenavi();
    }
wp_reset_query();
endif; ?>

It's really important that you use wp_reset_query(); to reset the main query after the loop.

Upvotes: 1

Related Questions