logo

$5
Static front page with both Page and Post content.

I am trying to create the following scenario using a static front page (using the accompanying template named 'Home').

Basically, I have a Page called 'Home' which contains some content which is displayed in the left hand side of the page inside the 'content-left' div tag. The below code loop works fine for this and all is well.

The problem I have is I wish to display the most recent 5 posts (titles and excerpts only) in the right hand side of the page in the 'content-right' div tag as well.

I assume I need a second loop in this div tag but have no idea how to do it. I'm looking for the loop/code to drop inside the 'content-right' div to display 5 most recent posts.

Please let me know if you require any more information.


<?php
/*
Template Name: Home
*/
?>

<?php get_header(); ?>

<div id="content">
<div id="content-left">

<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>

</div>
<!-- End Content Left -->

<div id="content-right">

LOOKING FOR THE CODE/LOOP TO GO IN HERE TO DISPLAY 5 MOST RECENT POSTS (TITLE AND EXCERPT ONLY)

</div>
<!-- End Content Right -->

</div>
<!-- End Content -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>

lowercase | 02/23/10 at 2:31pm | Edit


(3) Possible Answers Submitted...

  • avatar
    Last edited:
    02/23/10
    3:24pm
    Michael Fields says:

    This should do it.

    <?php
    $latest = get_posts('numberposts=5');
    foreach( $latest as $post ) {
    setup_postdata( $post );
    $url = get_permalink( $post->ID );
    $title = apply_filters( 'the_title', get_the_title() );
    $excerpt = apply_filters( 'the_excerpt', get_the_excerpt() );
    print "\n\t" . '<h2><a href="' . $url . '">' . $title . '</a></h2>';
    print "\n\t" . $excerpt;
    }
    ?>

    Previous versions of this answer: 02/23/10 at 2:53pm

    • 02/23/10 3:11pm

      lowercase says:

      That worked great but for one thing - any way to make the '[...]' that is returned after each post excerpt to be like a link (eg 'MORE' link)?

    • 02/23/10 3:19pm

      Michael Fields says:

      This should do it:

      add_filter('excerpt_more', 'new_excerpt_more');
      function new_excerpt_more( $more ) {
      global $post;
      $url = get_permalink( $post->ID );
      return '... <a href="' . $url . '">more</a>';
      }
      $latest = get_posts( 'numberposts=5' );
      foreach( $latest as $post ) {
      setup_postdata( $post );
      $url = get_permalink( $post->ID );
      $title = apply_filters( 'the_title', get_the_title() );
      $excerpt = apply_filters( 'the_excerpt', get_the_excerpt() );
      print "\n\t" . '<h2><a href="' . $url . '">' . $title . '</a></h2>';
      print "\n\t" . $excerpt;
      }

  • avatar
    Last edited:
    02/23/10
    2:54pm
    Erez S says:

    use the function wp_query and create new query and then simply use the loop.

  • avatar
    Last edited:
    02/23/10
    2:55pm
    Kim Parsell says:

    Try the following code inside your content-right div:


    <?php
    query_posts('showposts=5');
    if (have_posts()) : while (have_posts()) : the_post(); ?>
    <div>
    <h3 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h3>
    <?php the_excerpt(); ?>
    </div>
    <?php endwhile; else: endif; ?>
    <?php wp_reset_query() ?>


    I used an h3 for the post title - you'll have to adjust that to whatever works best with your theme.

    • 02/23/10 3:15pm

      lowercase says:

      This worked too - in a much simpler way. Any ideas about the below reply?

      That worked great but for one thing - any way to make the '[...]' that is returned after each post excerpt to be like a link (eg 'MORE' link)?

This question has expired.





Current status of this question: Completed