I Want to Add My Latest 5 Post Title with Corresponding Link in My Header Position.What was the Actual Php code?Am Newbie ....


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

|improve this answer|||||
  • it working fine.thx Daniel – Ramkumar M Jun 20 '11 at 13:38
  • 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.

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

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.