WordPress.org

Ready to get started?Download WordPress

Forums

displaying separate posts based on sequential order (4 posts)

  1. harberg
    Member
    Posted 2 years ago #

    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

  2. Digital Raindrops
    Member
    Posted 2 years ago #

    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

  3. harberg
    Member
    Posted 2 years ago #

    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?

  4. Digital Raindrops
    Member
    Posted 2 years ago #

    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

Topic Closed

This topic has been closed to new replies.

About this Topic