Sign up ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

2 Answers 2

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:

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 } ?> 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.