• Resolved morgyface

    (@morgyface)


    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

Viewing 15 replies - 1 through 15 (of 29 total)
  • Where are you setting up $paged?

    Thread Starter morgyface

    (@morgyface)

    @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

    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).

    Thread Starter morgyface

    (@morgyface)

    @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!

    Thread Starter morgyface

    (@morgyface)

    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.

    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;

    Thread Starter morgyface

    (@morgyface)

    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.

    Is this the only loop on the page?

    Thread Starter morgyface

    (@morgyface)

    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 } ?>

    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.

    Thread Starter morgyface

    (@morgyface)

    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?

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

    Thread Starter morgyface

    (@morgyface)

    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 } ?>

    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;?>
    Thread Starter morgyface

    (@morgyface)

    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!

Viewing 15 replies - 1 through 15 (of 29 total)
  • The topic ‘front-page wp-query pagination issue, repeating posts next_posts_link HELP.’ is closed to new replies.