Custom Post Type ‘single-custom.php’ next and previous links stuck in loop

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:

Read More
<?php next_post_link('%link', 'next item &gt;') ?>
<?php previous_post_link('%link', '&lt; 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 &gt;') ?></div>
<div class="prev-posts pull-left"><?php previous_post_link('%link', '&lt; 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><<< &quot;'. $prev_title . '&quot;</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>&quot;'. $next_title . '&quot; >>></strong></a>' . "n";
                    }
    ?>
    </div>

Related posts

Leave a Reply

2 comments

  1. <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><<< &quot;'. $prev_title . '&quot;</strong></a>' . "n";
                    }
    ?>
    </div>
    
    <div class="next-posts pull-right">
    <?php
    $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>&quot;'. $next_title . '&quot; >>></strong></a>' . "n";
                    }
    ?>
    </div>
    
  2. 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:

    <?php if ( (get_adjacent_post(false, '', false)) ) { ?>
        <div class="next-posts pull-right"><?php next_post_link('%link', 'volgend item &gt;') ?></div>
    <?php } elseif ( (get_adjacent_post(false, '', true)) ){ ?>
        <div class="prev-posts pull-left"><?php previous_post_link('%link', '&lt; vorig item') ?></div>
    <?php } ?>