Support » Plugins » Next/Previous X posts on single.php

  • Resolved WiBu

    (@wibu)



    I’ve been searching around and trying to hack in my own solution but I’ve had no success so I’m asking for some help.

    I’m trying to display the next/previous X posts on single.php in relation to the post that is being displayed. Almost exactly how previous/next_post_link currently work, but I want to be able to display the next/previous X posts.

    Thanks in advance.

Viewing 15 replies - 1 through 15 (of 16 total)
  • This plugin has always worked for me:

    WP Page Numbers

    Thanks for the reply but it’s not exactly what I’m looking for (I’m already using that for my archive pages).

    I’m looking for something for the single post page that allows me to list the next 5 posts and the previous 5 posts.

    I’m not sure if it can be done via a loop query, if it can, I haven’t found it.

    Use two query_posts loops

    1. Get date of current single post
    2. Using that date, and the Time parameters example, get all the posts before the current single post date, then cycle through and display 5 of those posts
    3. Follow a similar strategy from step 2 to get 5 posts dated after the current single post date.

    Thanks for the hints but I’m probably more lost than I was before I attempted to get something out of that. I’m a dab hand with html and js but php is a lot art for me at the moment.

    Unless someone can pretty much write it up for me, or give me a close to complete example, it looks like I’ll have to scale back what I wanted to achieve.

    Well this was fun:

    <?php
    //for use in single.php (if used other places need to set $currentid)
    //get all posts and then display certain number ($num_old_new) of posts dated before this post, and same number of posts dated after this post.
    //
    $num_old_new = 5; // max number of older posts to show.  Also will be max number of newer posts to show.
    $older_title = '<h2>Some older posts to consider</h2>';
    $newer_title = '<h2>Some newer posts to consider</h2>';
    $currentid = $posts[0]->ID;
    $thisone = 0;
    
    $args=array(
      'orderby' => 'date',
      'order' => 'ASC',
      'post_type' => 'post',
      'post_status' => 'publish',
      'showposts'=>-1,
      'caller_get_posts'=>1
    );
    $allposts = get_posts($args);
    
    if ($allposts) {
      for ( $counter = 0; $counter <= count($allposts); $counter += 1) {
        if ( $allposts[$counter]->ID == $currentid ) {
          $thisone = $counter;
        }
      }
      $begin = 0;
      $end = count($allposts) - 1;
    
      if ( $thisone - $num_old_new >= $begin ) {
        $begin = $thisone - $num_old_new;
      }
    
      if ( $thisone + $num_old_new <= $end )  {
        $end = $thisone + $num_old_new;
      }
    
      for ( $counter = $begin; $counter <= $end; $counter += 1) {
        if ( $allposts[$counter]->ID != $currentid ) {
          $post = $allposts[$counter];
          setup_postdata($post);
    
          if ( $counter < $thisone && $older_title ){ //display older title once
            echo $older_title;
            $older_title = '';
          }
    
          if ( $counter > $thisone && $newer_title ){ //display newer title once
            echo $newer_title;
            $newer_title = '';
          }
          ?>
          <p><?php the_time('m.d.y') ?> <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
          <?php
        }
      }
    }
    wp_reset_query(); //just in case
    ?>

    [edit fixed code]

    Wow, totally unexpected but big thanks.

    Playing with it now. I’m sure I can find a way to not have the newer message not appear when there are no new posts. Will post back with what I come up with (if I come up with anything).

    Ok, having no luck.

    Also attempting to spit newer/older up and having no luck. Playing around with the code, removing different things to see what disappears, not being successful.

    Darn, you’re right. The problem is the count value assigned to $end. It should be:

    $end = count($allposts) - 1;

    I’ve fixed the code above.

    OK, updated code works perfectly.

    I hate to ask for one last thing, but I’m having no luck myself.

    How would I change the code so that the output is something along the lines of:

    <p>Previously: &bull; [Code for 1] &bull; [Code for 2] &bull; [Code for 3] &bull; [Code for 4] &bull; [Code for 5]</p><br />
    <p>Next: &bull; [Code for 1] &bull; [Code for 2] &bull; [Code for 3] &bull; [Code for 4] &bull; [Code for 5]</p>

    I’m sure it only involves a quick check, similar to the insertion of the titles, but I’m clueless as to how to do it. It’s essentially just adding a /p to the end of the output.

    Not sure what your are asking, but you should be able to play with these three lines and change to something like:

    $older_title = '<p>Some older posts to consider ';
    $newer_title = '</p><br />Some newer posts to consider ';
    
    &bull;<?php the_time('m.d.y') ?> <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>

    Might also have to add a </p> before

    }
    wp_reset_query(); //just in case

    That was the first thing I tried. It just ended up giving me problems with closing the tags out. I realise it could just be as simple as opening the p tag with the $newer_title and $older_title, the issue comes with closing them off without relying on either one existing; at the first post or last post where newer or older doesn’t exist.

    I was wondering if it was possible copy/manipulate this:

    if ( $counter < $thisone && $newer_title ){ //display newer title once
          echo $newer_title;
          $newer_title = '';
          }

    Using a similar method to count to insert a /p closing tag at the end of the generated lists.

    You can always test to see if the title is empty, because if it is empty it means it was used.

    if (!$newer_title) {
    echo 'something';
    }

    Genius. Thanks a lot (and I really mean it).

    Haven’t tested yet, but it seems like it’ll work. Slowly slowly learning PHP; I’ll try and work out where to put it.

    Edit:

    OK, one slight issue with the ordering. Is there any way to have it so it is ascending for the newer and descending for the older?

    As an update to the previous post. Formatting is all sorted but there is one last issue… and it’s the ordering.

    With this:
    <?php
    //for use in single.php (if used other places need to set $currentid)
    //get all posts and then display certain number ($num_old_new) of posts dated before this post, and same number of posts dated after this post.
    //
    $num_old_new = 5; // max number of older posts to show. Also will be max number of newer posts to show.
    $newer_title = '<p class="single top">Next';
    $older_title = '</p><p class="single top">Previously';
    $currentid = $posts[0]->ID;
    $thisone = 0;

    $args=array(
    'orderby' => 'date',
    'order' => 'ASC',
    'post_type' => 'post',
    'post_status' => 'publish',
    'showposts'=>-1,
    'caller_get_posts'=>1
    );

    $allposts = get_posts($args);

    if ($allposts) {
    for ( $counter = 0; $counter <= count($allposts); $counter += 1) {
    if ( $allposts[$counter]->ID == $currentid ) {
    $thisone = $counter;
    }
    }
    $begin = 0;
    $end = count($allposts) - 1;

    if ( $thisone - $num_old_new >= $begin ) {
    $begin = $thisone - $num_old_new;
    }

    if ( $thisone + $num_old_new <= $end ) {
    $end = $thisone + $num_old_new;
    }

    for ( $counter = $begin; $counter <= $end; $counter += 1) {
    if ( $allposts[$counter]->ID != $currentid ) {
    $post = $allposts[$counter];
    setup_postdata($post);

    if ( $counter > $thisone && $newer_title ){ //display newer title once
    echo $newer_title;
    $newer_title = '';
    }

    if ( $counter < $thisone && $older_title ){ //display older title once
    if (!$newer_title) { } else {$older_title = '<p class="single top">Previously';}
    echo $older_title;
    $older_title = '';
    }

    ?> • " rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?><?php

    }
    }
    }
    ?></p>
    <?php wp_reset_query(); //just in case
    ?>

    It orders the “Next” correctly but “Previous” is sorted in reverse chronological, the previous post is the furthers to the right instead of being next to the title “Previous”.

    Any ideas on how to sort that out or is it a flaw with how it’s all done?

    Any idea how to separate the two objectives? Having one do Next ordered in ascending and Previous ordered in descending?

    Well you’d have to use two different ‘for’ structures to iterate through $allposts backwards and forwards. But I’ll leave you or someone else to hassle with that.

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘Next/Previous X posts on single.php’ is closed to new replies.