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.

  1. First, open your template’s functions.php using any text editor.
  2. 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;
    }
  3. 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“).
  4. 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>
  5. 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: , ,

You may be interested in reading these articles

11 Comments so far...

  1. Abang Long

    Great! I really need this.
    Thanks a lot.

  2. Alex- The Blog Traffic Guy

    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!

  3. dicky

    @Alex -
    I don’t aware that this method can increase the search engine ranking. Is it because of the internal linking?

  4. alex

    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 “>

  5. alex

    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]

  6. dicky

    @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.

  7. alex

    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;

  8. alex

    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

  9. dicky

    @alex -
    I had read your article and now i get what you mean. Thanks for pointing this out.

  10. Rizzi

    Heyyy….thanks a lot….its pretty simple and very much functional

  11. dicky

    @Rizzi -
    I hope you like my tutorial and support me. Thanks!

Leave a Reply