• Hello All,

    I am trying to figure out how to display the three most recent posts in three separate areas of my main page. I have three boxes that will have the three most recent posts in them. Box 1 has the most recent, box 2 the second most recent, and box 3 the third most recent. When a new post is made, what is in the boxes will change. I only want to display one post per box.

    I have tried

    <?php
    $posts=get_posts('numberposts=3&offset=0');
    foreach ($posts as $post) :
    ?>
    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    <p><?php echo limit_words(get_the_excerpt(), '40'); ?></p>
    <a href="<?php the_permalink(); ?>">Read More >></a>
    <?php endforeach ?>

    This works until I reload the page and it keeps adding posts into the boxes. Any help would be great. Thank you very much.

    Harberg

Viewing 3 replies - 1 through 3 (of 3 total)
  • You need to add the posts to a div

    <?php
    $posts=get_posts('numberposts=3&offset=0'); $counter=0;
    foreach ($posts as $post) :
    $counter++; $class =($counter == 3) ? ' last' : '';	?>
    <div class="box<?php echo $class; ?>">
    	<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    	<p><?php echo limit_words(get_the_excerpt(), '40'); ?></p>
    	<a href="<?php the_permalink(); ?>">Read More >></a>
    </div>
    <?php endforeach; ?>

    Stylesheet:

    .box {
    	width: 30.75%;
    	margin-right: 3.8%;
    	float: left;
    	min-height: 1px;
    }
    .last {
    	margin-right: 0px;
    }

    HTH

    David

    Thread Starter harberg

    (@harberg)

    David,

    Thank you this is working great. There is one problem, no the get_excerpt is no longer showing up.

    Any ideas what caused this?

    The excerpt by default is set at 40, the excerpt does not need the <p> or echo, so you could change it to:

    <?php the_excerpt(); ?>

    Also after the endforeach, add a clear.

    <div style="clear:both:"></div>

    HTH

    David

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘displaying separate posts based on sequential order’ is closed to new replies.