Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've created a portfolio with single posts. When viewing the posts i'd like to show the next / previous 2 posts (portfolio items).

Currently i'm using the following code but only shows one post:

<?php $prevPost = get_previous_post(true);
    if($prevPost) {?>
    <?php $prevthumbnail = get_the_post_thumbnail($prevPost->ID, array(114,114) );}?>
    <?php previous_post_link('%link',"$prevthumbnail <h3>%title</h3>", TRUE ); ?>

I want to create the following:

[next post] | [next post] | [current post (active state)] | [previous post] | [previous post].

Does anyone know how to change this to a working code?

share|improve this question
add comment

3 Answers 3

This one is not a good way to do it but it's easy to implement

global $post;
$original = $post;
$next = get_next_post(true);
$prev = get_previous_post(true);
$post = $next;
next_post_link();
$post = $original;
next_post_link();
// display current post
previous_post_link();
$post = $prev;
previous_post_link();
$post = $original;

To make the code more efficient, you can copy the respective functions from core code in "wp-includes/link-template.php" & then modify them as required

At the most basic level, in get_adjacent_post, change LIMIT 1 to LIMIT 2 & $wpdb->get_row to $wpdb->get_results, then all other function which directly/indirectly use this function will be changed accordingly

NOTE: Don't change the core code, instead copy the functions to your theme/plugin, rename them, modify them, then use the new functions to display the links

share|improve this answer
add comment

get_previous_post() and get_next_post() both use get_adjacent_post(). The function determines the next/previous post by inspecting the global $post object. You could alter the $post object to achieve your goal:

$current_post = $post; // keep a copy of the current post;

$post = get_previous_post(false); 
echo $post->post_title;
$post = get_previous_post(false);
echo $post->post_title;

$post = $current_post;

$post = get_next_post(false); 
echo $post->post_title;
$post = get_next_post(false);
echo $post->post_title;
share|improve this answer
    
Thanks @daniel looks like this is what I need.. BUT as I'm not a php programmer (sorry only front end skills), how do I transform this code into echo this: [code]<a href="get_post_link"><thumb><title><a>[/code] –  rwzdoorn Nov 7 '12 at 10:48
add comment

Had to mix up some stuff, but this is my result:

This can be handy for more people who want to create a portfolio with a strong next/previous system.

Thanks all for the help!

<?php
$current_post = $post; // keep a copy of the current post;

$post = get_previous_post(false); 
$attr = array(
                                'title' => $post->post_title,
                                'alt' => $post->post_title,
                                'class' => 'meta_icon'
                            );
echo "<a href='".$post->guid."'>";
echo "<div class='meta_apps'>";
echo "<div class=\"meta-icon\">".get_the_post_thumbnail($post->post_id, 'thumbnail', $attr)."</div>";
echo "<div class=\"meta-title\">".$post->post_title."</div>";
echo "</div>";
echo "</a>";

$post = get_previous_post(false);
$attr = array(
                                'title' => $post->post_title,
                                'alt' => $post->post_title,
                                'class' => 'meta_icon'
                            );
echo "<a href='".$post->guid."'>";
echo "<div class='meta_apps'>";
echo "<div class=\"meta-icon\">".get_the_post_thumbnail($post->post_id, 'thumbnail', $attr)."</div>";
echo "<div class=\"meta-title\">".$post->post_title."</div>";
echo "</div>";
echo "</a>";

$post = get_previous_post(false);
$attr = array(
                                'title' => $post->post_title,
                                'alt' => $post->post_title,
                                'class' => 'meta_icon'
                            );
echo "<a href='".$post->guid."'>";
echo "<div class='meta_apps'>";
echo "<div class=\"meta-icon\">".get_the_post_thumbnail($post->post_id, 'thumbnail', $attr)."</div>";
echo "<div class=\"meta-title\">".$post->post_title."</div>";
echo "</div>";
echo "</a>";

$post = $current_post;
$post = get_next_post(false);

$attr = array(
                                'title' => $post->post_title,
                                'alt' => $post->post_title,
                                'class' => 'meta_icon'
                            );
echo "<a href='".$post->guid."'>";
echo "<div class='meta_apps'>";
echo "<div class=\"meta-icon\">".get_the_post_thumbnail($post->post_id, 'thumbnail', $attr)."</div>";
echo "<div class=\"meta-title\">".$post->post_title."</div>";
echo "</div>";
echo "</a>";

$post = get_next_post(false);
$attr = array(
                                'title' => $post->post_title,
                                'alt' => $post->post_title,
                                'class' => 'meta_icon'
                            );
echo "<a href='".$post->guid."'>";
echo "<div class='meta_apps'>";
echo "<div class=\"meta-icon\">".get_the_post_thumbnail($post->post_id, 'thumbnail', $attr)."</div>";
echo "<div class=\"meta-title\">".$post->post_title."</div>";
echo "</div>";
echo "</a>";



// $post = get_next_post(false); 
// echo $post->post_title;
// $post = get_next_post(false);
// echo $post->post_title;

get_post_thumbnail();
next_post_link();
previous_post_link();
?>
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.