WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How-to show last 5 posts (5 posts)

  1. misterixas111
    Member
    Posted 2 years ago #

    How-to show last 5 posts that are published after current post?

    Explained on image below:

    IMAGE FOR EXPLAINE

  2. esmi
    Theme Diva & Forum Moderator
    Posted 2 years ago #

  3. misterixas111
    Member
    Posted 2 years ago #

    Thanks, but I don't think that only with get_posts i will resolve my primary problem.

  4. keesiemeijer
    moderator
    Posted 2 years ago #

    Try it with this in your theme's functions.php:

    function custom_prev_posts($limit = 5){
      global $wpdb, $post;
      $html = '';
      $prev_posts = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE $wpdb->posts.post_date < '%s' AND $wpdb->posts.post_type = 'post' AND $wpdb->posts.post_status = 'publish' ORDER BY $wpdb->posts.post_date DESC LIMIT $limit", $post->post_date ) ); 
    
      if($prev_posts){
        $html .= '<ul>';
        foreach ( $prev_posts as $prev_post  ) {
        $html .= '<li><a href="' . get_permalink( $prev_post->ID ) . '">' .$prev_post->post_title . '</a></li>';
        }
        $html .= '</ul>';
      }
      return $html;
    }

    And call the function inside the loop in single.php:

    <?php echo custom_prev_posts(); ?>

    Or if you want 2 previous posts:

    <?php echo custom_prev_posts(2); ?>
  5. misterixas111
    Member
    Posted 2 years ago #

    Thaaanks keesiemeijer, You are the best of ze best :) Nice work :)

Topic Closed

This topic has been closed to new replies.

About this Topic