• Hi guys

    I’m building a custom WordPress theme at the moment and I’m having trouble doing something which I thought originally must be quite simple.

    On a single post view page, I want to also feature the previous and next posts in the same category relative to the one I’m showing. At the moment all I seem to be able to do is include a link to them which is fine, but I want to be able to show anything from the post, the_title, the_content, the_permalink etc etc just like the main post on that page. This would give me full control over what I show.

    Basically I want to show a cut down preview of the previous and next posts, either side of the current post on a single post view. This could be used to cycle through the posts in that category, but offers a bit more of a clue about the destination of the link (ie show an image)…

    I thought I might be able to use a query and then run another loop to show them, but I haven’t managed to make this work. I’m also intrigued by the ‘offset’ parameter – could this help me?

    Does anyone know where I can start?

Viewing 1 replies (of 1 total)
  • After a few thoughts on this, I decided the easiest way would be the following:

    open up link-template in wp-includes, and find the following:
    function adjacent_post_link($format, $link, $in_same_cat = false, $excluded_categories = '', $previous = true)

    This is the function that gets the next and previous post titles. I think prior to 2.5 it was called something else, maybe one of the next_post etc functions, but i might be wrong.

    Now in that function find this:

    if ( !$post )
    return;
    $title = $post->post_title;
    if ( empty($post->post_title) )
    $title = $previous ? __('Previous Post') : __('Next Post');

    If you change the two instances of post_title to post_content you will have the next and last content displayed.(I couldn’t get post_excerpt to work as I use the more tag, and didn’t have time to put in some text in the excerpt field to test any further)

    There are a few problems with this though, the content is not clickable to actually take you to that post, so you would need to use next_post_link to access the title, however, those functions just reference the one we just changed, so we can’t do that. The next step therefore (which I think I might do next week), is to turn this into a plugin, allowing you to define if you want the content, excerpt, or maybe get an image from that post, as well as having the title displayed.

    Have a play and see what you think, I have a few other ideas(involving SQL queries), but this looks like the simplest.

    Alex

Viewing 1 replies (of 1 total)
  • The topic ‘Showing the previous and next posts in full’ is closed to new replies.