next_post_link() not working for single custom post type post

I can’t for the life of me get this to work – this is the code in my single-osu_work.php file which is used for this post type, but I can’t get the next and previous post links to appear – can you see a problem? I’ve tried putting the pagination links within the loop but still nothing.

Thanks,

Read More

Osu

if (have_posts()) : while (have_posts()) : the_post(); ?>
    <article <?php post_class(); ?> id="post-<?php the_ID(); ?>">
        <h1 id="mainheading"><?php the_title(); ?></h1>
        <?php the_content(); ?>
        <footer class="postmetadata">Date: <?php the_date('Y'); ?></footer>
    </article>
    <?php endwhile;
else : ?>
    <article id="nada">
        <h2 id="mainheading">Nothing found</h2>
        <p>Apologies, but you've come across a page that can't be found.</p>
        <p>Please use the search function or continue browsing.</p>
        <?php echo get_search_form(); ?>
    </article>  
<?php endif; ?>

    <div class="next-posts"><?php next_post_link('%link', 'Newer entry &gt;', true) ?></div>
    <div class="prev-posts"><?php previous_post_link('%link', '&lt; Older entry', true) ?></div>
    <div class="clear">&nbsp;</div>

Related posts

3 comments

  1. Ok, I found the solution in terms of getting the links to appear, however, I needed to remove the ‘true’ argument for only moving between posts in the same category/taxonomy.

    Is that right that you can’t restrict the posts linked to so that they’re only the ones within the same taxonomy? Here’s the correct code and as vancoder says, it needs to appear within the loop (I placed this just before endwhile;):

    <div class="next-posts"><?php next_post_link('%link', 'Newer entry &gt;') ?></div>
    <div class="prev-posts"><?php previous_post_link('%link', '&lt; Older entry') ?></div>
    

    As this is a custom post type with a custom taxonomy, I assume the pagination is breaking because WordPress is looking for a ‘category’ as if these were normal posts? Is there any way to make this work so that only Work posts within my custom taxonomy are returned?

  2. As of WordPress 3.8 you can specify a taxonomy as the fifth parameter. In your case it would be something like:

    <?php previous_post_link( '%link', __( 'Older Entry &gt;', 'sometextdomain' ), true, '', 'somecustomtaxonomy' ); ?>
    <?php next_post_link( '%link', __( 'Newer Entry &gt;', 'sometextdomain' ), true, '', 'somecustomtaxonomy' ); ?>
    

    Check the Codex for more info
    http://codex.wordpress.org/Function_Reference/previous_post_link

  3. Make sure if you have made a custom wp_query call before the next_post_link() / previous_post_link() calling and has not reset the query to the default.

    In that case please use wp_reset_query after the use of custom wp_query.

Comments are closed.