Create A Latest Post Section At The Beginning Of Each Post
You may want to read our latest article Best posts on Bloghonour from Dec 2008
I get this idea from SmashingApps. They created a latest article section at the top of each article. You may refer to the screenshot below:

This will make sure those readers who reach your blog through search engine or referer will have a chance to read the latest article. So, how are you going to add this section? Do you need any plugin to do so? No, you don’t need any plugin to add this section. Simply follow the tutorial below, and i will create a custom function for you.
- First, open your template’s functions.php using any text editor.
- Place this code at the end of the file. (Before “?>“)
/* Get latest post */ function get_latest_post($pre="You maybe interested in our latest article") { global $wpdb; $sql = " SELECT P.ID, P.post_title FROM $wpdb->posts P WHERE P.post_status = 'publish' AND P.post_type = 'post' ORDER BY P.ID DESC LIMIT 1 "; $post = $wpdb->get_row($sql); $ret = $pre; $ret .= '<a href="'. get_permalink($post->ID).'" title="'. $post->post_title .'"> '. $post->post_title .'</a>'; echo $ret; } - Basically, the code will query the latest published post, get the permalink and post title, and then append it with some text (the default is “You maybe interested in our latest article“).
- Open your single.php and place this code before the “entry” div.
<div id="latest-post"> <p><?php get_latest_post("You may want to read our latest article"); ?></p> </div> - You may change the “You may want to read our latest article” to any text that you want. Leave it blank and you will use the default setting.
You may call this function anywhere in your article or change the style through your template’s stylesheet.
Tags: custom function, post, tutorial





















Abang Long
November 19th, 2008
Great! I really need this.
Thanks a lot.
Alex- The Blog Traffic Guy
November 20th, 2008
Hmm… I think this is very useful for every blog. It will increase the search engine ranking too. Great tips and thanks for the detail tutorial!
dicky
November 21st, 2008
@Alex -
I don’t aware that this method can increase the search engine ranking. Is it because of the internal linking?
alex
November 24th, 2008
Personally I prefer to use the WP_Query function. It’s much nicer and doesn’t require any SQL syntax
have_posts()) : $my_query->the_post();
?>
<a href=”" rel=”bookmark” title=”Permanent Link to “>
alex
November 24th, 2008
Well obviouslly the code didn’t paste properly, just look up the WP_Query function and you’ll get what I mean.
[code]
have_posts()) : $my_query->the_post();
?>
<a href=”" rel=”bookmark” title=”Permanent Link to “>
[/code]
dicky
November 24th, 2008
@alex -
Sorry i don’t get what you mean. Do you mean we can use WP_query to replace the sql command? I did used WP_query before, but i don’t think it is suitable in this case.
alex
November 25th, 2008
yes, wp_query creates a separate loop
so something like the php code below will give you a link to the latest post.
$my_query = new WP_Query(’showposts=1′);
while ($my_query->have_posts()) : $my_query->the_post();
the_permalink();
endwhile;
alex
November 25th, 2008
Sorry for all the incomplete code snippets above.
I have written a post explaining how to achieve the same results, but in a more correct fashion using WP_Query
using WP_Query to display latest post
dicky
November 25th, 2008
@alex -
I had read your article and now i get what you mean. Thanks for pointing this out.
Rizzi
November 28th, 2008
Heyyy….thanks a lot….its pretty simple and very much functional
dicky
November 28th, 2008
@Rizzi -
I hope you like my tutorial and support me. Thanks!