WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Page navigation broken (20 posts)

  1. miromiro
    Member
    Posted 6 years ago #

    In Reading/options, I have set display pages to '10' - which works fine on Search returns, Archives, Category pages.

    On index.php I have used this snippet

    <?php query_posts('posts_per_page=2'); ?>

    To restrict the posts displayed on the homepage to 2 - works a treat. However, since implementing this, the previous_posts nav on the bottom of the homepage no longer works:

    <?php next_posts_link('&laquo; Older entries') ?>

    now points to the correct place, ie.,

    http://www.examplesite.com/blog/page/2/

    but serves up the index.php page again.

    Any help greatly appreciated.

  2. fool4Christ
    Member
    Posted 6 years ago #

    When you say it displays index.php again, I'm not sure what you mean.

    Is it only displaying two previous posts, as opposed to ten?

    If that's the case, perhaps a conditional in index.php is in order, something along the lines of this:

    <?php if ( is_home() ) query_posts('posts_per_page=2'); else query_posts(); ?>

  3. miromiro
    Member
    Posted 6 years ago #

    Thanks fool4Christ,

    I meant that it goes to ../blog/page/2/ but it serves the current index.php. ie., the 2 most current posts - not the older posts you would expect to find on page 2.

    So, you could keep clicking the link, but would only ever remain on the 'home' page...

    And the conditional didn't work - but thanks for the suggestion.

    Cheers

  4. fool4Christ
    Member
    Posted 6 years ago #

    Okay, I'm not out of ideas just yet.

    Try this, taking advantage of the WordPress template hierarchy:

    Create a home.php template file with the modified index.php code copied into it -- the code to call the two most recent posts.

    In index.php, place the default post display code.

    This way, whenever the main page is displayed, home.php will be used. On paged displays, index.php should be used.

  5. fool4Christ
    Member
    Posted 6 years ago #

    Oh, forgot this. You can get more information on which templates are used when here:
    http://codex.wordpress.org/Template_Hierarchy

  6. miromiro
    Member
    Posted 6 years ago #

    Awesome - I'll give it a go: thanks for the help.

    Cheers

  7. miromiro
    Member
    Posted 6 years ago #

    Nice idea, but no change...

    If it helps, full page source is here:

    http://pastebin.ca/699206

    Thanks again.

  8. fool4Christ
    Member
    Posted 6 years ago #

    Which file is that, home.php or index.php ?

    Try this conditional statement:

    <?php if ( is_home() && !is_paged() ) { query_posts('posts_per_page=2'); } ?>

    That should load the "limit to 2" bit only if you're on the home page and not one of the pages of the "home archive."

  9. miromiro
    Member
    Posted 6 years ago #

    Thanks for your patience, fool4Christ - it is the index.php.

    I tried the conditional and it worked in one sense, the nav now points to older posts. Problem though, instead of pointing to the 3rd oldest post (1 and 2 being on the front page), it points back to the 9th! How does that work?

    Cheers,

  10. fool4Christ
    Member
    Posted 6 years ago #

    Shoot. I didn't think about that! Quite sorry. As I had it above, the conditional is loading every other page as normal -- in other words, the second page will load the second ten pages. Only the first page's display is affected, hence the problem.

    And indeed, using the two different templates I mentioned above would result in the same problem, unless some code was inserted into the index.php loop to instruct WordPress to recognize that the second page still needs to grab 8 posts into the most recent 10, rather than skipping to the 11th.

    Phew.

    I'm not entirely sure how that PHP would be constructed, so if someone else has any ideas, feel free to chime in! I'll poke around and see what I can find, but it's not a problem I've come across before and I'm by no means a PHP guru.

    Thank *you* for your patience with me, miromiro!

  11. fool4Christ
    Member
    Posted 6 years ago #

    Okay, miromiro, I found the solution! It's as convoluted as can be, so please forgive that! I've used the code live on my blog just to make sure it works, and it does. The beauty of it is that it will work whether you have posts per page set to anything, not just 10, in the admin panel -- I hope. :)

    Check it out and let me know how it works for you:
    http://pastebin.com/f318db5f3

    I tried to comment it as best I could.

  12. miromiro
    Member
    Posted 6 years ago #

    Wow! The generosity of the people in this forum is always amazing, but fool4Christ you have blown me away with your commitment to helping a brother out - thank you.

    Your solution works beautifully.

    One final question - and I am really not expecting any other help on this - what do I do about this snippet?

    `<?php while (have_posts()) : the_post();

    if ($i == 1) { ?>
    <img src="<?php bloginfo('template_directory');?>/images/fresh.gif" id="fresh" alt="Newest Post" title="This post is guaranteed to be freshly published"/>
    <?php }
    ?>

  13. miromiro
    Member
    Posted 6 years ago #

    Wow! The generosity of the people in this forum is always amazing, but fool4Christ you have blown me away with your commitment to helping a brother out - thank you.

    Your solution works beautifully.

    One final question - and I am really not expecting any other help on this - what do I do about this snippet?

    `<?php while (have_posts()) : the_post();

    if ($i == 1) { ?>
    <img src="<?php bloginfo('template_directory');?>/images/fresh.gif" id="fresh" alt="Newest Post" title="This post is guaranteed to be freshly published"/>
    <?php } ?>

    It is placing the fresh icon at the top of every page, not just next to the newest post.

    Thanks again for all your help.

  14. fool4Christ
    Member
    Posted 6 years ago #

    Try this:

    <?php while (have_posts()) : the_post();
    
    if ($i == 1 && is_home() ) { ?>
    <img src="<?php bloginfo('template_directory');?>/images/fresh.gif" id="fresh" alt="Newest Post" title="This post is guaranteed to be freshly published"/>
    <?php } ?>

    That should cause the image only to be called on the home page's first post, I think.

  15. miromiro
    Member
    Posted 6 years ago #

    Thanks fool4Christ, but no change.

    I appreciate the effort, though :-)

  16. fool4Christ
    Member
    Posted 6 years ago #

    Sorry about that, miromiro! Try giving this one a whirl:

    <?php while (have_posts()) : the_post();
    
    if ( is_home() ) {
    if ( $i == 1 ) { ?>
    <img src="<?php bloginfo('template_directory');?>/images/fresh.gif" id="fresh" alt="Newest Post" title="This post is guaranteed to be freshly published"/>
    <?php } } ?>

    I'm not for sure why the first example won't work, but maybe this one will.

  17. miromiro
    Member
    Posted 6 years ago #

    Alas, no. Don't sweat it, fool4Christ: I am really thrilled that it is working as is, the fresh icon is not a big deal. Thanks again.

  18. fool4Christ
    Member
    Posted 6 years ago #

    One last try? lol!

    if ( is_home() && !is_paged() ) {
    if ( $i == 1 ) { ?>
    <img src="<?php bloginfo('template_directory');?>/images/fresh.gif" id="fresh" alt="Newest Post" title="This post is guaranteed to be freshly published"/>
    <?php } } ?>
  19. miromiro
    Member
    Posted 6 years ago #

    Ta da! It works - perfectly. Thanks fool4Christ - perhaps you could turn it into a plugin for others who would like to achieve the same thing? ;-)

    Really appreciate all the hours you have put into this.

    Cheers!

  20. fool4Christ
    Member
    Posted 6 years ago #

    I guess it isn't exaggeration if I say I fully believe in, "If it doesn't work the first time, keep guessing till it does."

    Glad you finally got it working! :D

Topic Closed

This topic has been closed to new replies.

About this Topic