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.

Live site

In the lower portion of the landing page, I'd like there to be three blog post previews- just the headline and thumbnail for the three most recent posts. I'm using the following code:

<?php get_header(); ?>

<div id="container">
    <div id="landing">
        <div id="intro">
            <h1>Hi, I'm Dylan Elchami-<br />a health and fitness enthusiast with a passion for inspiring.</h1>
            <p>My interests are vast and ever changing, but my virtues remain true. I love to motivate, create, make people healthier, stronger, sexier, and flat out better all while building great, unmitigated relationships along the way. Please roam my site and enjoy all the information I post for you, and if you have a question please don't hesitate to contact me- it's your way of giving back for all the free information because helping you is what makes me feel AWESOME.</p>
        </div><!-- end intro -->

        <div id="stay-in-touch">
            <p>Stay in touch-</p>
            <ul>
                <li><a href="#"><img src="<?php echo get_template_directory_uri(); ?>/img/facebook.png" alt="Like on Facebook" /></a></li>
                <li><a href="#"><img src="<?php echo get_template_directory_uri(); ?>/img/twitter.png" alt="Follow on Twitter" /></a></li>
                <li><a href="#"><img src="<?php echo get_template_directory_uri(); ?>/img/linkedin.png" alt="Connect on LinkedIn" /></a></li>
            </ul>
        </div><!-- end stay-in-touch -->

        <div id="preview">
            <div id="preview-item">
                <div id="preview-headline">
                    <a href="<?php the_permalink(); ?>"><?php the_title(); ?>
                </div><!-- end preview-headline -->
                <div id="preview-thumbnail">
                    <?php $my_query = new WP_Query( array( 'posts_per_page' => 3, 'nopaging' => true ) ); while ( $my_query->have_posts() ) : ( $my_query->the_post() ); ?>
                    <?php endwhile; ?> 
                </div><!-- end preview-thumbnail -->
            </div><!-- end preview-item --> 
        </div><!-- end preview-->
    </div><!-- end landing -->

<?php get_sidebar(); ?>

<?php get_footer(); ?>

I have add_theme_support( 'post-thumbnails' ); in functions.php

As you can see on the site, the link where the previews should be says "Home" and doesn't have a thumbnail. How do I show the most recent 3 posts as previews and have them actually link to the post(not home)?

share|improve this question
    
Do you have a second loop to call those 3 posts? Otherwise your loop will only contain the current post (in this case, Home). –  SickHippie May 21 '12 at 22:52
    
I do not, this is my first attempt at something like this which explains the issues/questions. Could you elaborate? –  user1255049 May 21 '12 at 22:56
    
Do you have the same problem with published posts, or only with draft/preview posts? –  Chip Bennett May 22 '12 at 18:16
    
@ChipBennett my only concern is with displaying published posts, which @SickHippie has helped me achieve. I am now having the issue of too many posts being displayed despite using array( 'posts_per_page' => 3,. –  user1255049 May 22 '12 at 18:45
    
"I am now having the issue of too many posts being displayed..." - that's a separate issue, and should probably be posted as a separate question. –  Chip Bennett May 22 '12 at 18:58
add comment

1 Answer

up vote 1 down vote accepted

It sounds like you need a second loop on your home page to call the 3 most recent posts. Try something like this:

// Main home page content here

<?php $my_query = new WP_Query( array( 'posts_per_page' => 3, 'nopaging' => true ) );
while ( $my_query->have_posts() ) : ( $my_query->the_post() ); ?>
<div id="preview">
    <div id="preview-item">
        <div id="preview-headline">
        <a href="<?php the_permalink(); ?>"><?php the_title(); ?>
    </div><!-- end preview-headline -->
    <div id="preview-thumbnail">
        <?php the_post_thumbnail( array(120,102) ); ?> 
    </div><!-- end preview-thumbnail -->
    </div><!-- end preview-item --> 
</div><!-- end preview-->
<?php endwhile; ?>

// Closing Home page content here
share|improve this answer
    
Thanks. I'm using the following code to no avail: <div id="preview"> <div id="preview-item"> <div id="preview-headline"> <a href="<?php the_permalink(); ?>"><?php the_title(); ?> </div><!-- end preview-headline --> <div id="preview-thumbnail"> <?php $my_query = new WP_Query( array( 'posts_per_page' => 3, 'nopaging' => true ) ); while ( $my_query->have_posts() ) : ( $my_query->the_post() ); ?> <?php endwhile; ?> </div><!-- end preview-thumbnail --> </div><!-- end preview-item --> </div><!-- end preview--> What could be the prob? –  user1255049 May 22 '12 at 16:45
    
You don't have anything during the while statement, so there won't be any changes. Your code isn't actually doing anything. Move your entire preview code block from your original post into the while statement, like I have in my post (updated). –  SickHippie May 22 '12 at 16:50
    
I appreciate all of your help. I guess what's troubling me now is the fact that these previews will be on a statice page- there isn't dynamic content/a loop for me to insert the code you've given in.. unless I'm misunderstanding what you're saying? I've edited my original post to show you exactly what I'm working with. –  user1255049 May 22 '12 at 17:55
    
Please look at my updated post. I've posted the code in the way it needs to be displayed. Wrap your entire preview area while the while/endwhile section - that's the loop. Anything between those two lines will be repeated for each item in the loop, which is limited to 3 by posts_per_page. Think of it like this - WP_Query gets all the posts. while says "for each item in this variable $my_query do whatever I say until endwhile. I'll update my original post to be hopefully less confusing. –  SickHippie May 22 '12 at 18:13
    
Again, thank you for your patience and help. If you click the link in my original post, you'll see that it now functions but is showing 4 posts despite having array( 'posts_per_page' => 3,. Any ideas on how to rectify this? –  user1255049 May 22 '12 at 18:25
show 4 more comments

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.