0

I'm trying to display the excerpt of the most recent post and just the titles of the 4 previous posts before that at the top of a page and display the content of the page below. I have a solution that's close and works but not sure if I need to reset query and how to get excerpt on most recent post and title on 4 previous. Here's the page with post titles at top (B's recommended articles) and the content (that can be edited) of the page is underneath. Here's the code http://pastebin.com/41x30Uyb

1 Answer 1

0

To use template tags within a foreach loop outputting the results of get_posts, use the function setup_postdata() to populate the global $post with the data it needs. To style the first post differently, use a counter to keep track of which post you're outputting. After finishing output, use wp_reset_postdata() to restore the $post global. wp_reset_query is only necessary if you modify the global $wp_query.

$posts = get_posts( 'category=124&numberposts=5' );
$count = 1;
foreach( $posts as $post ) {
    setup_postdata($post);
    if( 1 == $count ){
        the_title();
    } else {
        the_excerpt();
    }
    $count++;
}
wp_reset_postdata();
1
  • I tweaked it a little bit. Changed the excerpt and title positions (I wanted the most recent as excerpt then the previous 4 as titles) I also added a title link above the excerpt and date function after each title - thank you. You can see it here rabbib.com/bs-recs Commented Mar 14, 2013 at 2:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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