I'm trying to display the title and excerpt of the most recent 8 posts on the sidebar.

I'm getting a list where:

  • All items link to the oldest post
  • All titles are "id number of a post that should be displayed"+"oldest post title"
  • All excerpts are from the oldest post

        $args = array( 'numberposts' => '8' );
        $recent_posts = wp_get_recent_posts( $args );
        $noticias_highlight = true;

        foreach( $recent_posts as $recent ){ 
                    <div class="entry <?php if ($noticias_highlight) echo 'highlight'; $noticias_highlight = !$noticias_highlight; ?>">
                        <div class="title"><p><a href="<?php the_permalink($recent["ID"]); ?>"><?php the_title($recent["ID"]); ?></a></p></div>
                        <div class="subtitle"><?php the_excerpt($recent["ID"]); ?></div>

Don't use the 'helper' methods, they tend to cause more trouble than they're worth.

Any time you want to grab posts, be it the latest, the oldest, in a category etc, use a WP_Query loop, here is its standard form:

$query = new WP_Query( $args );
if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        // do stuff
} else {
    // none were found

This is what wp_recent_posts will be doing internally, although it isn't doing it very well. So save the above to an autocomplete macro in your editor


Many mistakes in the question's code:

  • the_permalink() takes no arguments and returns the link of the current post. Use echo get_permalink() instead.
  • the_title() prints the title of the current post, and its first argument is a string to be printed before the title. That's what causing the titles to be all the same but containt the numeric IDs for the correct posts. Use echo get_the_title() instead.
  • the_excerpt() takes no arguments and prints the excerpt of the current post. I don't know if there's a similar function that accepts he post ID as an argument.

Most importantly:

  • wp_get_recent_posts() will return an array containing one array for each post (hence the foreach). So instead of the_title() or get_the_title() the title can be returned via $array[post_title], the excerpt via $array[post_excerpt] (will be empty if no excerpt is present, won't automatically truncate the post content), $array[post_content] for the post content, etc.
  • wp_get_recent_posts() doesn't seem to use a wordpress loop. So functions that use the current post will always use the same post. That's what happens in the example.

And even most importantly:


You can use the Display Posts shortcode in your sidebar markup. The plugin is here: http://wordpress.org/plugins/display-posts-shortcode/

  • 1
    I prefer not using a plugin for simple functionality that can be achieved in a few lines of code. – That Brazilian Guy Aug 12 '13 at 18:12
  • 1
    Not everyone who reads wordpress.stackoverflow lives in the PHP code. Some folks are just interested in getting things done. Why penalize alternate solutions? – dthorpe Aug 12 '13 at 18:21

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.