Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

<?php
        $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>
                    </div>
<?php
        } 
?>
share|improve this question

3 Answers 3

up vote 2 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() ) {
        $query->the_post();
        // do stuff
    }
    wp_reset_postdata();
} 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
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

 
discard

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.