Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

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?

share|improve this question
    
Thanks for the edit, couldn't post pictures, not enough rep :) –  Tilen Krivec May 5 '11 at 17:45

3 Answers 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.

share|improve this answer
    
If you use query_posts() in your template file directly you could confuse the "main loop", if you display it after this part. To prevent that, you should use WP_Query or get_posts instead of query_posts. –  Jan Fabry May 5 '11 at 13:12
1  
Don't use is_home() here, as that would be true when the Blog Posts Index is displayed. This is the Front Page, so use is_front_page(). –  Chip Bennett May 5 '11 at 13:21
    
Thanks, wiil try it out and let you know how it works –  Tilen Krivec May 5 '11 at 17:45

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.

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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