Next / Prev post link not working wp

Ok so I am having issues with linking to next and previous posts…

Here is my code:

Read More
<?php get_header(); ?>

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
...

<div id="project-prev"> <?php previous_post_link('Prev'); ?> </div>
<div id="project-next"> <?php next_post_link('Next'); ?> </div>]

...

<?php endwhile; // end of the loop. ?>
<?php endif; ?>
<?php get_footer(); ?>

I have read places that next/prev posts requires a ‘new WP_Query’ query, but have had no such luck. There is no next/prev link rendered out on my site, using the above.

As always appreciate solutions and pointers.

Many thanks

Related posts

Leave a Reply

3 comments

  1. Have you tried following (according to WordPress codex)

     <?php next_post_link('<strong>%link</strong>'); ?> 
     <?php previous_post_link('<strong>%link</strong>'); ?> 
    

    In your divs … 🙂 If you still encouter problems, then just try something like:

    <?php echo get_previous_posts_link('Prev'); ?>
    <?php echo get_next_posts_link('Next'); ?>
    

    Should work.

    EDIT:

    <div id="project-prev"><?php previous_post_link('%link', 'PREV'); ?></div>
    <div id="project-next"><?php next_post_link('%link', 'NEXT'); ?></div>
    
  2. First try and get the next and previous posts.

    <?php
    $previous_post_url = get_permalink(get_adjacent_post(false, '', true));
    $next_post_url = get_permalink(get_adjacent_post(false, '', false));
    ?>
    

    And then create to <a> tags and echo out the URLs we set above.

    <?php if ( $previous_post_url != get_the_permalink() ) : ?>
        <a href="<?php echo $previous_post_url; ?>">Previous Project</a>
    <?php endif; ?>
    
    <?php if ( $next_post_url != get_the_permalink() ) : ?>
        <a href="<?php echo $next_post_url; ?>">Next Project</a>
    <?php endif; ?>
    
  3. One reason that could make these links not to show is having the posts set as draft. Only published posts will make the previous and next post links render.