Displaying Latest Posts on a Page

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.

Read More

Thanks, Wade

Related posts

Leave a Reply

1 comment

  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,
                    post_date,
                    post_title,
                    post_excerpt
            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' 
                    AND
                    post_password = '' 
                    AND
                    post_type = 'post' 
            GROUP BY 
                    ID,
                    post_date,
                    post_title,
                    post_excerpt
            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;
    }