WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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>
share|improve this question
up vote 5 down vote accepted

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

share|improve this answer

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:

share|improve this answer

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

share|improve this answer
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
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 posting your answer, you agree to the privacy policy and terms of service.

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