I am trying to create a loop to display the title and excerpt of each blog post onto a page that isn't my index page. The in-progress demo site is here: http://thewestharbour.com/mypakage/

On the news page I need to display the latest 3 blog posts. Simply changing the settings won't work and every loop I have tried doesn't work either. I have also tried creating a separate template and assigning it to the news page but had no luck.

Thanks, Wade

  • Try a search for "wordpress display recent posts on page"
    – Brent
    Commented Aug 16, 2012 at 0:53

1 Answer 1


You can modify your post template (usually index.php, but depends on your theme) to:

  1. check if the current request should response those posts
  2. add a segment which will query the database to retrieve the posts you wish
  3. echo the post title and excerpt as you wish

The following code will query the database and return a string with the 5 latest posts with excerpts. Note the $before, $after, $before_excerpt, $after_excerpt, these are just wrappers for the content to make it look nicer, you can send all these as a parameter to the function, but for the sake of simpleness, I hard-coded these.

function get_posts()
    global $wpdb;

    $post_count = 5;
    $before = '<h3>';              // this will be rendered before the post content
    $after = '<br />';             // this will be rendered after the post content
    $before_excerpt = '</h3><p>';  // this will be rendered before the post excerpt
    $after_excerpt = '</p>';       // this will be rendered after the post excerpt

    $request =
        select  ID,
        from    $wpdb->posts               p inner join
                $wpdb->term_relationships  r on r.object_id = p.ID inner join
                $wpdb->terms               t on t.term_id = r.term_taxonomy_id
        where   post_status = 'publish' 
                post_password = '' 
                post_type = 'post' 
        GROUP BY 
        ORDER BY
                post_date DESC
        LIMIT   0, $post_count

    $posts = $wpdb->get_results($request);
    $output = '';

    if ($posts)
        foreach ($posts as $post)
            $post_title = $post->post_title;
            $permalink = get_permalink($post->ID);

            $output .= $before;
            $output .= '<a href="' . esc_url($permalink) . '" rel="bookmark" title="Permanent Link: ' . esc_attr($post_title) . '">' . esc_html($post_title) . '</a>';

            $post_excerpt = esc_html($post->post_excerpt);
            $output .= $before_excerpt . $post_excerpt . $after_excerpt;

            $output .= $after;

    return $output;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.