I’ve got a custom post type in WordPress called ‘cases’. For the single page I’ve created a single-case.php, which all works fined but one thing:
Right outside the While loop I’m trying to have a ‘Next’ and ‘Previous’ link, like so:
<?php next_post_link('%link', 'next item >') ?>
<?php previous_post_link('%link', '< previous item') ?>
But they get stuck in a loop. Once it reaches a certain article WordPress gets stuck and loops the same two articles over and over.
Here’s the full (stripped down) template:
... Header ...
<?php while (have_posts()) : the_post(); ?>
<div class="row content">
<div class="span4">
<?php if(count($aImages)) : ?>
<?php foreach($aImages as $aImage) : ?>
....
<?php endforeach; ?>
<?php endif; ?>
</div>
<div class="span7">
<div class="case-title"><?php the_title();?></div>
<?php the_content(); ?>
</div>
</div>
<?php endwhile; ?>
... Footer ...
<div class="next-posts pull-right"><?php next_post_link('%link', 'volgend item >') ?></div>
<div class="prev-posts pull-left"><?php previous_post_link('%link', '< vorig item') ?></div>
UPDATE
For some reason this fixes my issue:
<div class="prev-posts pull-left">
<?php
$prev_post = get_previous_post();
if($prev_post) {
$prev_title = strip_tags(str_replace('"', '', $prev_post->post_title));
echo "t" . '<a rel="prev" href="' . get_permalink($prev_post->ID) . '" title="' . $prev_title. '" class=" "><strong><<< "'. $prev_title . '"</strong></a>' . "n";
}
?>
</div>
<div class="next-posts pull-right">
<?
$next_post = get_next_post();
if($next_post) {
$next_title = strip_tags(str_replace('"', '', $next_post->post_title));
echo "t" . '<a rel="next" href="' . get_permalink($next_post->ID) . '" title="' . $next_title. '" class=" "><strong>"'. $next_title . '" >>></strong></a>' . "n";
}
?>
</div>
next_post_link is meant to be called from within the loop.enter link description here. Since this is for a single post, you can safely move the next_post_link and previous_post_link lines into the loop.
UPDATE:
Not sure why you’re getting that behavior, but you can add something like this in: