WordPress: Show Previous Post Permalink on Home Page with Single Post

WordPress:  Show Previous Post Permalink on Home Page with Single Post
0 Flares Twitter 0 Facebook 0 StumbleUpon 0 Google+ 0 LinkedIn 0 Buffer 0 Reddit 0 Email -- 0 Flares ×

WordPressMore WordPress fun this weekend. I had a pile of work to do, and got caught babysitting this weekend (such is the life of a Single Dad). We single parents stick together and watch each other’s kids whenever there’s the opportunity. So… I entertained the kids and then stayed up in the wee hours getting some work done.

First, we launched Black In Business for a client. I customized the theme from the Beckett theme. It’s integrated with Google Analytics and preloaded with a nice selection of WordPress Plugins.

Next, I worked on a custom hack for Blaugh. It winds up that when you have a single post on your Index page, it’s difficult to bring back the previous post’s permalink. So, I rummaged around and found the answer:

  1. Here’s a custom function that will pull back the previous post. You use it within the Loop. In other words, after:
    >?php if (have_posts()) : while (have_posts()) : the_post(); ?>

  2. You must upload the my-hacks.php (attached file) to your WordPress directory - not the theme directory. If you already have one, DO NOT write over it, just copy the function and add it to the file:

    function get_last_posts($no_posts = 5, $before = '>li>', $after = '>/li>', $show_pass_post = false, $skip_posts = 0) {
    global $wpdb, $tableposts;
    $request = "SELECT ID, post_title FROM $tableposts WHERE post_status = 'publish' ";
    if(!$show_pass_post) { $request .= "AND post_password ='' "; }
    $request .= "ORDER BY post_date DESC LIMIT $skip_posts, $no_posts";
    $posts = $wpdb->get_results($request);
    $output = '';
    foreach ($posts as $post) {
    $post_title = stripslashes($post->post_title);
    $permalink = get_permalink($post->ID);
    $output .= $before . '>a href="' . $permalink . '" rel="bookmark" title="Permanent Link: ' . $post_title . '">' . $post_title . '>/a>' . $after;
    echo $output;

  3. In Options > Miscellaneous, make check the box:
    Use legacy my-hacks.php file support
  4. Where ever you would like the last post permalink to be shown, insert the following: >? get_last_posts(1, '', '', true, 1) ?>

This basically tells the query, pull back the latest post and skip the last one. So you wind up getting the second latest post. This is the post you guys want! I tested this fully on my blog and it works great.