Forums

[resolved] query_posts() Pagination - tried all fixes, still can't work it out. (20 posts)

  1. dcoa
    Member
    Posted 10 months ago #

    Hi All,
    First of all I realise that I am posting about a well documented issue, but I have spent hours and hours trying to fix this, and I apologise in advance if it is an easy fix that I have missed.

    So I have a custom query_posts function on the site that is simply meant to include posts of a certain category. This works fine, but when the posts exceed one page the second page is the exact same as the first. The code I am using at the moment is

    <?php
    $temp = $wp_query;
    $wp_query= null;
    $wp_query = new WP_Query();
    $wp_query->query('showposts=5&&cat=4'.'&paged='.$paged);
    ?>
    <?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
    
    <div class="post" id="post-<?php the_ID(); ?>">
    <div class="post-title">
    <h2>
    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent link to <?php the_title(); ?>">
    <?php the_title(); ?></a>
    </h2>
    </div>
    
    <div class="post-content">
    <?php the_content("Continue Reading"); ?>
    </div>
    
    <?php endwhile; ?>
    
    <div class="post-nav">
    <p><?php posts_nav_link('∞','&laquo;&laquo; Previous Page','Next Page &raquo;&raquo;'); ?></p>
    </div>
    
    </div>
    <?php $wp_query = null; $wp_query = $temp;?>

    I have also tried the solution
    here
    with no luck.
    The posts display fine with both, buy no pagination.

    Site is here if it helps.

    Any help would be much appreciated. Have been pulling my hair out over this for a while.

  2. keesiemeijer
    moderator
    Posted 10 months ago #

    Try it with this in your code:

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $wp_query->query('posts_per_page=5&cat=4&paged='.$paged);
  3. dcoa
    Member
    Posted 10 months ago #

    Hi, Thanks for the reply, unfortunately still no luck, implemented your code as such:

    <?php
    $temp = $wp_query;
    $wp_query= null;
    $wp_query = new WP_Query();
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $wp_query->query("posts_per_page=5&cat=4&paged=".$paged);
    while ($wp_query->have_posts()) : $wp_query->the_post();
    ?>

    But no change to the problem, still the same thing on first and second page.

  4. dcoa
    Member
    Posted 10 months ago #

    Additionally, If I echo $paged, It prints as "1" both on the first and second page, so that must be what the problem is. (Not sure if there is a better way to check that? like a trace statement or something?

  5. keesiemeijer
    moderator
    Posted 10 months ago #

    If this is on a static front page try:

    <?php
    if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
    elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
    else { $paged = 1; }
    ?>
  6. dcoa
    Member
    Posted 10 months ago #

    It is a static front page, I tried that code but unfortunately still nothing. I tried echoing the various values on the page and

    get_query_var('page') returns nothing, no value

    get_query_var('paged') returns "1" on both the first and second page.

    $paged returns 1 as well.

    Would this mean that the code is right but something is amiss elsewhere?

    also, If I change the else statement to
    else { $paged = 2; }
    It then loads only second page, and wont go back to the first, does this meant that the if and elseif statements aren't being satisfied?

    thanks for your help so far

  7. keesiemeijer
    moderator
    Posted 10 months ago #

    Do you have multiple loops on this page? Try putting this after the first loop: wp_reset_query();

    http://codex.wordpress.org/Function_Reference/wp_reset_query

  8. dcoa
    Member
    Posted 10 months ago #

    I do have a bit of code that keeps the static page information at the top of the posts. I tried your suggestion as well as completely deleting the extra code, just leaving the bare bones of the loop and unfortunately it is still not working. Could it be something not on this page that is failing? Sorry to be such a trouble, I really do appreciate your help.

  9. keesiemeijer
    moderator
    Posted 10 months ago #

    Can you paste the content of the Page template file of your static front page in a pastebin, submit it and give us a link to it.

  10. dcoa
    Member
    Posted 10 months ago #

  11. dcoa
    Member
    Posted 10 months ago #

    I have tried removing everything in lines 13-28, to see if they were causing a problem, but that didn't fix it.

  12. keesiemeijer
    moderator
    Posted 10 months ago #

    That seems ok. try it with <?php wp_reset_query(); ?> after the endwile:

    <?php endwhile; else: endif; ?>
    <?php wp_reset_query(); ?>
  13. keesiemeijer
    moderator
    Posted 10 months ago #

    and try with a single "&":
    change:

    $wp_query->query('showposts=5&&cat=4'.'&paged='.$paged);

    to:

    $wp_query->query('posts_per_page=5&cat=4&paged='.$paged);
  14. dcoa
    Member
    Posted 10 months ago #

    Thanks, I did try with that in there but took it back out because it didn't work, just put it back in to be sure and unfortunately still nothing.

  15. dcoa
    Member
    Posted 10 months ago #

    Sorry, still not working, I forgot I changed that back from the code you gave me, but just tried it with $wp_query->query('posts_per_page=5&cat=4&paged='.$paged); back in and still the same problem.

  16. keesiemeijer
    moderator
    Posted 10 months ago #

    I,ve changed your page template a bit. template

    This is not tested so make a backup. And you have to change the Page ID in the first loop.

  17. alchymyth
    The Sweeper
    Posted 10 months ago #

    i don't want to interrupt the conversation - just something to consider:

    referring to the code in pastebin http://pastebin.com/5gx1Nr4Y

    - catching the $paged parameter quite at the start of the code, seems to work in my test setup:

    <?php get_header(); ?>
    
    <div id="content" class="large">
    
    <?php $paged = get_query_var('paged');  ?>
    
    <?php if (have_posts()) : ?>

    and obviously deleting this section:

    if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
    elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
    else { $paged = 1; }
  18. dcoa
    Member
    Posted 10 months ago #

    Ok Fixed!
    The problem was that get_query_var('paged') doesn't seem to work when a page template is used on a static page on the homepage, So you have to use get_query_var('page') instead, but for some reason having this being called after the $temp = $wp_query; value was redefined meant it didn't return a value. So I just had to move it up so it was called before the custom value was defined. Working code for anyone with the same problem is:

    <?php
     $paged = (get_query_var('page')) ? get_query_var('page') : 1;
    $temp = $wp_query;
    $wp_query= null;
    $wp_query = new WP_Query();
    
    $wp_query->query('posts_per_page=5&cat=4&paged='.$paged);
    while ($wp_query->have_posts()) : $wp_query->the_post();
    ?>

    Then:
    <?php endwhile; ?> after the post.`
    Then:
    <?php $wp_query = null; $wp_query = $temp;?>
    at the end.

    Full page code <a href="http://pastebin.com/N0dEngEf">here</a>

    This <?php print_r ($wp_query->query_vars); ?> was very useful in working out which variable was returning which value.

    Thanks keesiemeijer for your patience and help.

  19. dcoa
    Member
    Posted 10 months ago #

    Thanks again guys, I actually didn't get a chance to look at the last two posts above mine, were posted while I was writing my 'Ok Fixed!' anyone with the same problem as me might want to try those as well, but I am punching out, been trying to fix this for way too long.

  20. keesiemeijer
    moderator
    Posted 10 months ago #

    This code is only there because it is a static front page

    if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
    elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
    else { $paged = 1; }

    You can reduce it to: $paged = (get_query_var('page')) ? get_query_var('page') : 1;

    Glad you got it resolved.

Reply

You must log in to post.

About this Topic