Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Static front page with both Page and Post content. WordPress

  • SOLVED

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(); ?>

Answers (3)

2010-02-23

Michael Fields answers:

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;
}
?>


lowercase comments:

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)?


Michael Fields comments:

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;
}

2010-02-23

Erez S answers:

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

2010-02-23

Kim Parsell answers:

Try the following code inside your <strong>content-right</strong> 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.


lowercase comments:

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)?