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 Want to Add My Latest 5 Post Title with Corresponding Link in My Header Position.What was the Actual Php code?Am Newbie ....

share|improve this question
up vote 4 down vote accepted

This sounds like an additional Loop on that page, right? You might want to use:

    <?php $posts_query = new WP_Query('posts_per_page=5');
        while ($posts_query->have_posts()) : $posts_query->the_post();
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endwhile; wp_reset_query(); ?>

Also read more here: http://codex.wordpress.org/The_Loop#Multiple_Loops_in_Action

share|improve this answer
it working fine.thx Daniel – Ramkumar Jun 20 '11 at 13:38
you're welcome :) – Daniel Sachs Jun 20 '11 at 13:41
Note that this method incurs unnecessary overhead, because, IIRC, posts_per_page doesn't limit the number of posts returned, but rather only limits the number of posts shown per page. So, even though you're only showing 5 posts, the query returns all posts. Thus, get_posts() is considerably lighter for returning recent posts. – Chip Bennett Jun 20 '11 at 14:56
<?php $recent_posts = get_posts('numberposts=5');
if($recent_posts) { ?>
        <?php foreach( $recent_posts as $recent ) { ?>
        <a href="<?php echo get_permalink($recent->ID); ?>"><?php echo $recent->post_title; ?></a>
        <?php } ?>
<?php } ?>

http://codex.wordpress.org/Template_Tags/get_posts you need to change the html tags if you need a different output.

share|improve this answer
thx michael.... – Ramkumar Jun 20 '11 at 13:37
I prefer the get_posts() method, personally. – Chip Bennett Jun 20 '11 at 14:52

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.