Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter misterixas111

    (@misterixas111)

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

    Moderator keesiemeijer

    (@keesiemeijer)

    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); ?>

    Thread Starter misterixas111

    (@misterixas111)

    Thaaanks keesiemeijer, You are the best of ze best ?? Nice work ??

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How-to show last 5 posts’ is closed to new replies.