Adding “latest from the blog” to the homepage

I have a big problem and no clue how to solve it 🙂 I want to add a “module” to my homepage (static homepage, not blog posts) that will display the 2 latest blog posts in a very particular way and link back to them. The posts would only display on the homepage and not every other page but the php template used for the page is shared with all the custom pages.

Here is a quick sketch of it

Read More

Photo of a paper sketch

A site that does this beautifully on their homepage is SEOmoz ( but i don’t need that much functionality, only blog post title, thumb, excerpt & link to read more )

Screenshot from seomoz.org

Is there a simple way to do this? (plugins, code you can copy/paste from anywhere?) or can I pay someone to write this up for me?

Related posts

Leave a Reply

3 comments

  1. Personally, I like using get_posts() (Codex ref) for quick-and-dirty Loops.

    In your front-page.php template file, try the following:

    <?php
    
    // Create a variable to hold our custom Loop results
    $frontpageposts = get_posts( array( 
         'numberposts' => 2 // only the 2 latest posts
    ) );
    
    // Create output only if we have results
    // Customize to suit your HTML markup
    if ( $frontpageposts ) { 
    
         foreach ( $frontpageposts as $fppost ) { 
              // setup postdata, so we can use template tags
              setup_postdata($fppost);
              ?>
    
              <div <?php post_class(); ?>>
                   <h2><a href="<php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                   <div class="post-entry">
                        <?php the_post_thumbnail(); ?>
                        <?php the_excerpt(); ?>
                   </div>
              </div>
    
    <?php }
    } 
    ?>
    

    Again, you’ll need to modify the HTML markup according to your needs.

  2. An even easier way to do this is to make a page-x.php file in your theme. X is the ID of the page you’re going to use as a homepage. This assumes you have FTP access which you must since you can create files. For instance, the page-2.php would be the template used for the default WordPress sample page. But you can’t just use if (have_posts()) since you’re on a page and that will pull up the pages content that you’re on.

    It would be better to use a WP_Query.

    $news = new WP_Query( array(
                               'category_name'=>'news',
                               'posts_per_page' => 2
                               )
                        );
    
    if ($news->have_posts()) while ($news->have_posts()) : the_post(); ?>
    

    Then continue with your loop. Let me know if you need more detail. Where I have

    'category_name' => 'news'
    

    ‘news’ should be changed to the SLUG of your category.

  3. Nice handwriting 🙂

    This can be simply done by checking whether the current page is home, and by fetching the two latest blog posts.

        <?php
            if ( is_home() ) {
    
                query_posts("posts_per_page=2");
            }
    
            // run the Loop
    
            if ( have_posts() ) : while ( have_posts() ) : the_post(); 
    
        ?>
    
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    
    <?php endwhile; ?>
    
    <?php endif; ?>
    

    This will show the blog post titles linked to the blog posts themselves. Insert any other function that will fetch the info you need (eg, the_excerpt() ).

    You can add parameters to your query in order to make it more precise, eg, retrieve posts from a specific category only.