• Hi,

    I’m using PageNavi plugin to handle pagination in my theme. However, it looks like it’s getting confused with URLs.

    The structure of my theme is as follows:
    /wordpress/ – homepage
    /wordpress/blog/ – blog page

    The pagination shows correctly on both the homepage and blog posts page. It works perfectly on the blog page: clicking “2” link in pagination returns /wordpress/blog/page/2 – and a second page of blog posts, of course.

    However, clicking “2” link in *homepage* pagination returns (correctly) /wordpress/page/2 but the content is the same as /wordpress/blog/page/2 (second page of blog posts).

    I’m using two different loops for homepage and blog page, like so:

    homepage:

    <?php
    	  $temp = $wp_query;
    	  $wp_query= null;
    	  $wp_query = new WP_Query('cat=-'.$blog_cat_id.'&paged=' . $paged);
    	?>
    
    	<?php if ($wp_query->have_posts()) : ?>
    	<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
    
    post stuff here
    
    <?php
    	  endwhile;
    	  if(function_exists('wp_pagenavi')) : ?>
    	  <div class="pagination"><?php if(function_exists('wp_pagenavi')) { wp_pagenavi(); } ?></div>
    	  <?php endif; $wp_query = null; $wp_query = $temp; ?>

    blog page loop is basically the same except the query doesn’t have the “-” in categories, returning posts from only blog category:
    $wp_query = new WP_Query('cat='.$blog_cat_id.'&paged=' . $paged);

    Is there a way to fix the pagination on the homepage?

    I will post the link to the theme if necessary, just ask for it if you need it, please.

Viewing 7 replies - 1 through 7 (of 7 total)
  • However, clicking “2” link in *homepage* pagination returns (correctly) /wordpress/page/2 but the content is the same as /wordpress/blog/page/2 (second page of blog posts).

    What posts would you expect to see in /wordpress/page/2 if not the same ones as on /wordpress/blog/page/2 ?

    I think the actual error here is that PageNavi (or WordPress) doesn’t output a link to /wordpress/blog/page/2 in both cases.

    On the homepage, try using a different variable like $my_query instead of $wp_query OR do $wp_query = $temp before calling wp_pagenavi():

    BTW: $wp_query = null is useless.

    The $wp-query stuff comes from the fact that PageNavi doesn’t really work with custom queries, as explained here: https://www.wplover.com/756/how-to-get-custom-wp_query-loop-working-with-pagination-and-wp-pagenavi

    Using this tutorial was the only way I managed to even get PageNavi to display correctly.

    Thing is, as explained, homepage and blog page use two different loops. So I would expect Page 2 of “blog” loop (/wordpress/blog/page/2) to display blog posts – which happens, all good.

    However, homepage has a different loop, so I would expect Page 2 of “homepage” loop (/wordpress/page/2 to display only homepage posts.

    Here’s the link to live example: https://dev.gentlecode.net/cssshow/wordpress/

    Homepage displays all posts EXCEPT blog category. Blog page displays ONLY posts in blog category.

    EDIT: It’s worth mentioning that PageNavi correctly renders the pagination on the homepage (it’s set to show only one posts per page for testing purposes) – there are 5 posts and it correctly displays 5 pages. It just seems to get confused with the URLs.

    The $wp-query stuff comes from the fact that PageNavi doesn’t really work with custom queries, as explained here: https://www.wplover.com/756/how-to-get-custom-wp_query-loop-working-with-pagination-and-wp-pagenavi

    You can achieve the same thing using query_posts() and wp_reset_query().

    However, homepage has a different loop, so I would expect Page 2 of “homepage” loop (/wordpress/page/2 to display only homepage posts.

    Well, in that case, make sure the correct loop is executed on /wordpress/page/2. You can add a line like this: echo "in the home loop";.

    You can achieve the same thing using query_posts() and wp_reset_query().

    That’s the problem. It doesn’t work with query_posts(). It gets really confused. This method was the only one that worked for me.

    Well, in that case, make sure the correct loop is executed on /wordpress/page/2. You can add a line like this: echo "in the home loop";.

    I think it’s pretty obvious that it doesn’t execute the correct loop on /wordpress/page/2. Otherwise, it wouldn’t display posts from a category that is excluded in that query.

    The whole point of this thread is me trying to figure out why it doesn’t execute the correct loop. I reset the query after both loops, everything works great (PageNavi correctly renders 5 links for 5 pages of posts) just not the actual links in the homepage pagination.

    If the loops are in the same file, post the entire code block that is relevant, i.e. including the if condition.

    Loops are in different files:

    homepage.php: https://tinypaste.com/7217d – the actual homepage template
    index.php: https://tinypaste.com/3f32e
    page.php: https://tinypaste.com/8d206 – posting in case it can be relevant to the problem.

    EDIT: I think it’s worth mentioning that the archives for both categories and tags paginate correctly as well:
    https://dev.gentlecode.net/cssshow/wordpress/tag/colors/page/2
    https://dev.gentlecode.net/cssshow/wordpress/category/designer/page/2

    You do need to call it wp_query.

    And my problem is that I wasn’t overwriting global $wp_query; Only $wp_query;

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘[Plugin: WP-PageNavi] PageNavi getting confused with URLs?’ is closed to new replies.