WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] front-page wp-query pagination issue, repeating posts next_posts_link HELP. (30 posts)

  1. morgyface
    Member
    Posted 1 year ago #

    Hello,

    I have created a page called "home". I have set that page as my front page.

    I have then edited page.php in my theme so that it basically asks which page it is on and then runs the loop with a $wp_query in front of it so that the correct posts are dished out and paginated. This works perfectly on every page apart from the home page.

    The home page dishes out the first page of posts perfectly but when clicking on next_posts_link the same posts are repeated again and again.

    I have spent the last 3 hours trying to find a solution but I have not found one. If you can help me I'll buy you a pint and a bag of crisps.

    Here is the code I am using within page.php

    <?php if ( is_page('home') ) { ?>
    <?php
    $temp = $wp_query;
    $wp_query= null;
    $wp_query = new WP_Query();
    $wp_query->query('cat=-6&showposts=6'.'&paged='.$paged);
    ?>
    <?php get_template_part( 'loop', 'index' );?>
    <?php } ?>

    Please help me, you're my only hope.

    Regards, Morgyface. x

  2. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    Where are you setting up $paged?

  3. morgyface
    Member
    Posted 1 year ago #

    @esmi Thank you for your reply.

    Where are you setting up $paged?

    I'm not sure I am setting up $paged explicitly but it seems to work everywhere apart from the home-page.

    The loop itself contains the code to generate the previous_posts_link and next_posts_link

  4. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    Try:

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $wp_query->query('cat=-6&showposts=6'.'&paged='.$paged);

    I'm also a little confused as to why you're not using query_posts() (if you're customising the main Loop query) or WP_Query() for a secondary query).

  5. morgyface
    Member
    Posted 1 year ago #

    @esmi Thank you again for your help.

    Okay so I inserted the code you provided above so it is now:

    <?php if ( is_page('home') ) { ?>
    <?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('cat=-6&showposts=6'.'&paged='.$paged);
    ?>
    <?php get_template_part( 'loop', 'index' ); ?>
    <?php } ?>

    But the problem persists unfortunately.

    I'm also a little confused as to why you're not using query_posts() (if you're customising the main Loop query) or WP_Query() for a secondary query).

    I'm as confused as you. I'm not a developer by any stretch of the imagination I'm more of a front end designer but have cobbled together snippets of code over the last couple of years and generally manage to get things working. If there's a better way of doing this, I'd be grateful for your help!

  6. morgyface
    Member
    Posted 1 year ago #

    Just for the record I tried changing my approach here on the basis that @esmi questioned why I wasn't using query_posts. I tried:

    <?php
    if ( is_page('home') || get_query_var('paged') ) { $paged = get_query_var('paged'); }
    elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
    else { $paged = 1; }
    
    query_posts('cat=-6&posts_per_page=6&paged=' . $paged); 
    
    // The Loop
    while ( have_posts() ) : the_post();
    	echo '<li>';
    	the_title();
    	echo '</li>';
    endwhile;
    
    previous_posts_link('&laquo; Previous');
    next_posts_link('More &raquo;');
    
    // Reset Query
    wp_reset_query();
    ?>

    The code generated the titles and the next_posts_link, but it suffered with the same problem my original approach did. Clicking the More link resulted in the page presenting the same 6 posts.

    I'm still desperate for a solution here.

  7. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    That's getting closer but just replace:

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

    with

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

  8. morgyface
    Member
    Posted 1 year ago #

    Thank you @esmi okay so the code now reads:

    <?php if ( is_page('home') ) { ?>
    
    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts('cat=-6&posts_per_page=6&paged=' . $paged); 
    
    // The Loop
    while ( have_posts() ) : the_post();
    	echo '<li>';
    	the_title();
    	echo '</li>';
    endwhile;
    
    previous_posts_link('&laquo; Previous');
    next_posts_link('More &raquo;');
    
    // Reset Query
    wp_reset_query();
    ?>
    <?php } ?>

    Which generates the same list as before the modification and suffers with the same pagination problem. Clicking Next just presents the same 6 post titles.

  9. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    Is this the only loop on the page?

  10. morgyface
    Member
    Posted 1 year ago #

    Hi esmi, thank you for persisting with this.

    Is this the only loop on the page?

    No there are multiple loops within page.php. They all follow a similar process. This works perfectly for every page apart from the front/home page which is why I'm completely baffled.

    For example:

    <?php if ( is_page('music') ) { ?>
    <?php
    $temp = $wp_query;
    $wp_query= null;
    $wp_query = new WP_Query();
    $wp_query->query('cat=6&showposts=6'.'&paged='.$paged);
    ?>
    <?php get_template_part( 'loop', 'index' );?>
    <?php } ?>
  11. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    Aha! You should only use query_posts() when modifying the main query on the page. All secondary queries should use WP_Query(). And you won't get working pagination for secondary queries - only the main query.

  12. morgyface
    Member
    Posted 1 year ago #

    Hi esmi, I don't understand? It does work though, on all pages apart from the home page? In theory there is only ever one WP_Query as they all have a condition of if is_page.

    Are you able to explain how I resolve this? I'm more than happy to try anything you can suggest to get it working. It just baffles me that the process works (with working pagination) for all the other pages and WP_Query's apart from the home page?

  13. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    So there is only one query & Loop n the site's front page?

  14. morgyface
    Member
    Posted 1 year ago #

    Hi esmi, sorry to be so stupid, I'm just not a developer so I'm not sure how to implement what you're suggesting. My apologies. It might also be useful to other people to spell this out as I've noticed this is a common problem with no obvious solution.

    Here's the fundamental code I have so far so you can tell me how to modify.

    <?php get_header(); ?>
    <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
    <?php the_content(); ?>
    <?php endwhile; ?>
    
    <?php
    /* The pagination does not work on this */
    if ( is_page('home') ) { ?>
    <?php
    $temp = $wp_query;
    $wp_query= null;
    $wp_query = new WP_Query();
    $wp_query->query('cat=-6&showposts=6'.'&paged='.$paged);
    ?>
    <?php get_template_part( 'loop', 'index' );?>
    <?php } ?>
    
    <?php
    /* The pagination DOES work on this */
    if ( is_page('music') ) { ?>
    <?php
    $temp = $wp_query;
    $wp_query= null;
    $wp_query = new WP_Query();
    $wp_query->query('cat=6&showposts=6'.'&paged='.$paged);
    ?>
    <?php get_template_part( 'loop', 'index' );?>
    <?php } ?>
  15. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    There are multiple queries/loops on that page. The top block:

    <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
    <?php the_content(); ?>
    <?php endwhile; ?>

    is the main Loop. So any other query/loop after that is secondary - even if they are wrapped in conditionals. Based on what you've said up until now, I'd guess that you want something like:

    <?php get_header(); 
    
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    if ( is_page('home') ) :
    query_posts('cat=-6&posts_per_page=6&paged=' . $paged);
    get_template_part( 'loop', 'index' );
    
    elseif( is_page('music') ):
    query_posts('cat=6&posts_per_page=6&paged=' . $paged);
    get_template_part( 'loop', 'index' );
    
    else:
    if ( have_posts() ) while ( have_posts() ) : the_post();
    the_content();
    endwhile;
    
    endif;?>
  16. morgyface
    Member
    Posted 1 year ago #

    Hi esmi, thanks for persevering with this... I used your code exactly as you presented it. And the same problem rears its ugly head.

    The posts on the home page are presented as you would expect, as is the "older posts" link. When you click on the "older posts" link the same 6 posts are repeated. The URL in the address bar reads website.com/page/2/ but it shows the same six posts as the first page.

    Bizarrely if you visit the 'music' page, the pagination works perfectly, no repeat, no issue. It seems to occur only on the home/front page.

    So although I might not have built the page in the proper manner it seems this is not the issue.

    Do you have any other ideas? I'd be so grateful if you have any other suggestions!

  17. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    Assuming by is_page(home'), you're referring to your site's front page, try replacing it with is_front_page().

  18. morgyface
    Member
    Posted 1 year ago #

    Hi esmi, that's how it was originally set up, I think I changed it yesterday whilst trying to find resolution, I just changed it back and tested it to be sure but the outcome is the same.

    Any other ideas?

  19. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    Can you post the whole page if it's not too long?

  20. morgyface
    Member
    Posted 1 year ago #

    I can indeed.

    <?php
    get_header(); ?>
    <div class="content">
    <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
    
    <?php
    if ( is_front_page() ) { soliloquy_slider( '152' ); } elseif ( is_page( 'music' ) ) { soliloquy_slider( '156' );
    } else ( the_title ('<div class="titlebar"><h1>', '</h1></div>') )
    ?>	
    
    <?php
    /* If is contact page deliver body text div and the content.
     */
     if ( is_page('contact') ) { ?>
     <div class="body-text">
     <?php the_content(); ?>
     </div>
    <?php } ?>	
    
    <?php endwhile; ?>
    
    <?php
    /* If the home page run the loop to output posts.
     * Though exclude Music posts (category id 6)
     */
     if ( is_front_page() ) { ?>
    
    <?php
    $temp = $wp_query;
    $wp_query= null;
    $wp_query = new WP_Query();
    $wp_query->query('cat=-6&showposts=6'.'&paged='.$paged);
    ?>
    
    <?php
    /* Run the loop to output the posts.
     * If you want to overload this in a child theme then include a file
     * called loop-index.php and that will be used instead.
     */
     get_template_part( 'loop', 'index' );
    ?>
    
    <?php } ?>
    
    <?php
    /* If the music page run the loop to output music posts.
     */
     if ( is_page('music') ) { ?>
    <?php
    $temp = $wp_query;
    $wp_query= null;
    $wp_query = new WP_Query();
    $wp_query->query('cat=6&showposts=6'.'&paged='.$paged);
    ?>
    
    <?php
    /* Run the loop to output the posts.*/
     get_template_part( 'loop', 'index' );
    ?>
    <?php } ?>	
    
    <?php
    /* If the articles page run the loop to output article posts.
     */
     if ( is_page('articles') ) { ?>
    <?php
    $temp = $wp_query;
    $wp_query= null;
    $wp_query = new WP_Query();
    $wp_query->query('cat=7&showposts=6'.'&paged='.$paged);
    ?>
    
    <?php
    /* Run the loop to output the posts */
     get_template_part( 'loop', 'index' );
    ?>
    <?php } ?>	
    
    <div class="clr"><hr /></div>
    </div>
    
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>
  21. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    I;m not 100% sure of what you're trying to do but the logic is all over the place in that code. Try something like:

    <?php
    get_header();
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    ?>
    
    <?php
    // set up custom queries
     if ( is_front_page() ) query_posts('cat=-6&showposts=6'.'&paged='.$paged);
     elseif( is_page('music') ) query_posts('cat=6&showposts=6'.'&paged='.$paged);
    elseif( is_page('articles') ) query_posts('cat=7&showposts=6'.'&paged='.$paged);
    ?>
    
    <div class="content">
    <?php if ( is_front_page() ) soliloquy_slider( '152' );
    elseif ( is_page( 'music' ) ) soliloquy_slider( '156' );?>	
    
    <?php if ( !is_page('music') && !is_page('articles') && !is_front_page() ) :
    // This is a standard static page
    the_title ('<div class="titlebar"><h1>', '</h1></div>')
    <div class="body-text">
    <?php the_content(); ?>
    </div>
    
    else:
    // This is a page of posts
    get_template_part( 'loop', 'index' );
    endif; ?>
    
    <div class="clr"><hr /></div>
    </div>
    
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>
  22. morgyface
    Member
    Posted 1 year ago #

    Thank you so much esmi. When pasted into my HTML editor it highlighted some syntax issues so, in the interests of testing, I simplified it to:

    <?php
    get_header();
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    ?>
    
    <?php
    // set up custom queries
     if ( is_front_page() ) query_posts('cat=-6&showposts=6'.'&paged='.$paged);
     elseif( is_page('music') ) query_posts('cat=6&showposts=6'.'&paged='.$paged);
    elseif( is_page('articles') ) query_posts('cat=7&showposts=6'.'&paged='.$paged);
    ?>
    
    <div class="content">
    <?php if ( is_front_page() ) soliloquy_slider( '152' );
    elseif ( is_page( 'music' ) ) soliloquy_slider( '156' );
    else ( get_template_part( 'loop', 'index' ) );
    ?>
    
    <div class="clr"><hr /></div>
    </div>

    The code is much simpler and more logical but, guess what, the problem continues. It displays the first 6 posts, yet when clicking on "Older posts" it repeats the first 6. The music page works as it should!

    How frustrating!

  23. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    That code won't work full stop on some pages.

    <?php if ( is_front_page() ) soliloquy_slider( '152' );
    elseif ( is_page( 'music' ) ) soliloquy_slider( '156' );
    else ( get_template_part( 'loop', 'index' ) );
    ?>

    will cause the front page & the music page to show nothing but a slider.

  24. morgyface
    Member
    Posted 1 year ago #

    I thought the same thing but upon upload it DID work (apart from the ongoing issue).

  25. vjpo
    Member
    Posted 1 year ago #

    try to use pre_get_posts in functions.php instead of query_posts()
    the last one is not recommended in this case.
    examples here
    http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

  26. keesiemeijer
    moderator
    Posted 1 year ago #

    If this is a "Page" template pre_get_posts will not work to alter the query.
    http://wordpress.stackexchange.com/questions/50761/when-to-use-wp-query-query-posts-and-pre-get-posts

    Use 'page' on a static front page and 'paged' on all other pages to get the query var.

    change this:

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

    to this (for static front page and other pages):

    if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
    elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
    else { $paged = 1; }
  27. vjpo
    Member
    Posted 1 year ago #

    this is a "Page" template

    Ah, had lost it, sorry.

  28. morgyface
    Member
    Posted 1 year ago #

    I want to thank @esmi for her persistence in trying to resolve this and @keesiemeijer for the final tip that nailed it.

    Although I've not yet modified the generic page.php template yet, the following code works perfectly, with the next 6 posts appearing as they should. It was a huge relief. I wish I could buy beer for everyone!

    <?php
    get_header();
    if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
    elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
    else { $paged = 1; }
    ?>
    <?php query_posts('cat=-6&showposts=6'.'&paged='.$paged); ?>
    <?php get_template_part( 'loop', 'index' );?>
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>

    Thanks again folks x

  29. unexploded
    Member
    Posted 1 year ago #

    Hi all, just wanted to say I had the same problem and from everyone's help, Morgyface's final post on this thread helped me out.

    Thanks!

  30. saurav2785
    Member
    Posted 11 months ago #

    HI all i was also stuck on the same issue but this Post really helped me resolve it...

    SO thanks for posting....

Topic Closed

This topic has been closed to new replies.

About this Topic