0

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?

1

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

0

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;
  • 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
0

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();
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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