I’ve got another question regarding next / previous post.
At the moment I’m working on our portfolio with several items. Each item is a post with a title, category & featured image. When viewing an item there are 4 next/previous posts.
How do I display the next 2 and previous 2? And If the next or previous don’t exist how do I show only 4 previous or 4 next posts (at the first and last post)?
Currently I’m using twitter bootstrap CSS/HTML:
<div class="row-fluid tab-footer">
<div class="span3">
<div class="row-fluid none">
<div class="span3">THUMBNAIL</div>
<div class="span9" ><h2>TITLE</h2>
<p><?php
foreach((get_the_category()) as $childcat) {
if (cat_is_ancestor_of(3, $childcat)) {
echo '<a href="'.get_category_link($childcat->cat_ID).'">';
echo $childcat->cat_name . '</a> ';
}}
?>
</p>
</div>
</div>
<div class="span3 hidden-phone">
<div class="row-fluid none">
<div class="span3">THUMBNAIL</div>
<div class="span9" ><h2>TITLE</h2>
<p>CATEGORY</p>
</div>
</div>
<div class="span3 hidden-phone">
<div class="row-fluid none">
<div class="span3">THUMBNAIL</div>
<div class="span9" ><h2>TITLE</h2>
<p>CATEGORY</p>
</div>
</div>
<div class="span3">
<div class="row-fluid none">
<div class="span3">THUMBNAIL</div>
<div class="span9" ><h2>TITLE</h2>
<p>CATEGORY</p>
</div>
</div>
</div>
You need to send a custom query to get a list of posts. In this example we use a custom post type “project” and a custom taxonomy “sphere” and we get 1 next project in current sphere. In your use case you can increase posts_per_page to 4.
Inside the while loop you can get the thumbnail with get_the_post_thumbnail(). You can find a few more lines of description in this blog post