WordPress Navigation Enhancements

WordPress Navigation Enhancements

I recently had the need to enhance our wedding cake blog to include excerpts of the previous and next posts when viewing a single post. In our case, our excerpt is just an image of the next cake. The code isn’t live yet, but I imagine it looking something like this:

Mockup

To do this I created a two main functions that get the next and previous posts excerpts. This code was then placed in my current theme directory in a file called functions.php. In the future I’ll look to release a plugin for this.

<?php

function pk_get_next_post($in_same_cat = false, $excluded_categories = ) {
        global $post, $wpdb;

        if(! is_single()) {
                return null;
        }

        $current_post_date = $post->post_date;
   
        $join = ;
        if ($in_same_cat) {
                $join = " INNER JOIN $wpdb->post2cat ON $wpdb->posts.ID= $wpdb->post2cat.post_id ";
                $cat_array = get_the_category($post->ID);
                $join .= ‘ AND (category_id = ‘ . intval($cat_array[0]->cat_ID);
                for ($i = 1; $i < (count($cat_array)); $i++) {
                        $join .= ‘ OR category_id = ‘ . intval($cat_array[$i]->cat_ID);
                }
                $join .= ‘)’;
        }

        $sql_exclude_cats = ;
        if (!empty($excluded_categories)) {
                $blah = explode(‘and’, $excluded_categories);
                foreach($blah as $category) {
                        $category = intval($category);
                        $sql_exclude_cats .= " AND post_category != $category";
                }
        }

        $now = current_time(‘mysql’);
   
        return @$wpdb->get_row("SELECT ID,post_title,post_excerpt FROM $wpdb->posts $join WHERE post_date > ‘$current_post_date’ AND post_date < ‘$now’ AND post_status = ‘publish’ $sqlcat $sql_exclude_cats AND ID != $post->ID ORDER BY post_date ASC LIMIT 1");
}

function pk_get_previous_post($in_same_cat = false, $excluded_categories = ) {
        global $post, $wpdb;

        if(! is_single()) {
                return null;
        }
   
        $current_post_date = $post->post_date;
        $join = ;
        if ($in_same_cat) {
                $join = " INNER JOIN $wpdb->post2cat ON $wpdb->posts.ID= $wpdb->post2cat.post_id ";
                $cat_array = get_the_category($post->ID);
                $join .= ‘ AND (category_id = ‘ . intval($cat_array[0]->cat_ID);
                for ($i = 1; $i < (count($cat_array)); $i++) {
                        $join .= ‘ OR category_id = ‘ . intval($cat_array[$i]->cat_ID);
                }
                $join .= ‘)’;
        }

        $sql_exclude_cats = ;
        if (!empty($excluded_categories)) {
                $blah = explode(‘and’, $excluded_categories);
                foreach($blah as $category) {
                        $category = intval($category);
                        $sql_exclude_cats .= " AND post_category != $category";
                }
        }

        return @$wpdb->get_row("SELECT ID, post_title, post_excerpt FROM $wpdb->posts $join WHERE post_date < ‘$current_post_date’ AND post_status = ‘publish’ $sqlcat $sql_exclude_cats ORDER BY post_date DESC LIMIT 1");
}

function pk_previous_post_excerpt($in_same_cat = false, $excluded_categories = ) {
  $prev_post = pk_get_previous_post($in_same_cat, $excluded_categories);

  if(! $prev_post) {
    return;
  }
  echo $prev_post->post_excerpt;

}

function pk_next_post_excerpt($in_same_cat = false, $excluded_categories = ) {
  $next_post = pk_get_next_post($in_same_cat, $excluded_categories);

  if(! $next_post) {
    return;
  }

  echo $next_post->post_excerpt;
}
 ?>
 

The next step would be to place a call to these two functions somewhere within your singlepost.php page. In my case I’m going to place them near the top and wrap them in some CSS. Here are the two functions you need to call:

pk_previous_post_excerpt();
pk_next_post_excerpt();
 

See Adam Holte’s implementation of the navigation on his photoblog.

4 Comments

  1. Is there any way to modify this to only show the most recent post in a particular category?

    Steve – Yes you would have to modify the sql to find the ID of the post with the MAX(POST_DATE) and then use that to get the info associate with the post

  2. Thanks for the code! I copied and pasted it and made it into a plugin just by adding a couple lines at top. When I copied it, the single quotes didn’t come out right, so I had to replace them, but it works on my soon to be published photoblog. I think having the image as a link would be very helpful. I haven’t tried to add that yet, but will.

  3. I’m trying to use this. I’ve followed you steps but I’m not doing something right.

    I’ve copied the code sample, created my functions.php, and uploaded everything to my site.

    How do I call the function in my single.php file?

    pk_previous_post_excerpt();

    I’m guessing I need to also include some type of reference to the functions.php file?

    Thanks for the help.

    Dorian – if I remember correctly any functions you add to functions.php are globally available. So all you should need to do is add a call to your function within single.php. Hope this helps

Leave a Reply

Your email address will not be published. Required fields are marked *