0

See www.pinosalon.com, Recent Blog is the same as Welcome.

On the page-home.php template that is used as the home page I have the loop which simply displays the Welcome text.

A few lines down the template, outside the loop, I get recent posts and display the latest post with the_excerpt(). The title and read more link work, but the_excerpt grabs the content from the loop rather than from the most recent post.

After updating the code based on the solution suggested by another question, the excerpt is no longer displayed.

Here is the code I am using:

//Block displays the current page's content
<?php while ( have_posts() ) : the_post(); ?>
<h1><?php the_title(); ?></h1>
    <?php the_content(); ?>
<?php endwhile; ?>

//Block that should display the latest post
<?php
$args = array( 'numberposts' => '1','post_type' => 'post' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ) : setup_postdata($recent);
    echo '<h3><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' .   $recent["post_title"].'</a></h3>';
echo the_excerpt();
echo '<a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >Read More</a>';
endforeach;
?>
  • I tried the solution given on the suggested question and it does not fit. It removed the excerpt completely. I've edited the code to reflect the change. – Alexnl Feb 7 '14 at 19:46
1

Your problem is that you're using $recent_posts as your variable name. If setup_postdata() isn't working, then your the_excerpt() should return whatever the excerpt is of the current page.

From the setup_postdata() codex page:

You must pass a reference to the global $post variable, otherwise functions like the_title() don't work properly.

So change this:

$args = array( 'numberposts' => '1','post_type' => 'post' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ) : setup_postdata($recent);

To this:

global $post;
$args = array( 'numberposts' => '1','post_type' => 'post' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $post ) : setup_postdata($post);

Better yet, use WP_Query which is perfect for secondary loops!

$args = array( 'posts_per_page' => '1','post_type' => 'post' );
$recent_posts = new WP_Query( $args );
if( $recent_posts->have_posts() ) : while( $recent_posts->have_posts() ) : $recent_posts->the_post();

And replace endforeach; with endwhile; endif;


Bonus best practice, never use page-{anything}.php for a template name as that's a reserved pattern in the template hierarchy. For a static front page, use front-page.php instead.)

  • Thanks for your help! I went with WP_Query. I've changed my template naming structure. Thanks for the additional advice! – Alexnl Feb 7 '14 at 20:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.