0

The site I am working on needs to have the returned url set a specific way. That being said, all posts are basically images that are posted, with there titles being said as the page urls. How do I go about getting the returned url from next_posts_link() and previous_posts_link to be returned as mysite.com/posttitle ?

At this point I have two divs wrapped with divs and spans. located within the loop

  <div class="back-nav">     
                <span class="button-nav-previous">
                 <?php previous_posts_link( '',0); ?>
                </span>
            </div>

currently this is returning mysite/title/page/2.

1 Answer 1

4

The functions you are using refer to the next or prev paginated set of posts, not a single post, hence the format they are in. Try using get_adjacent_post() instead.

<?php
    $prev = get_adjacent_post(false, '', true)
    $next = get_adjacent_post(false, '', false)

    //use an if to check if anything was returned and if it has, display a link
    if($prev){
        $url = get_permalink($prev->ID);            
        echo '<a href="' . $url . '" title="' . $prev->post_title . '">Previous Post</a>';
    }

    if($next) {
        $url = get_permalink($next->ID);            
        echo '<a href="' . $url . '" title="' . $next->post_title . '">Next Post</a>';
    } 
?>

You can read more about get_adjacent_posts() here.

This should be noted that the get_adjacent_post() function is intended to be used on a single detail page. If you are using this on a posts page it may behave unexpectedly.

2
  • Basically it'll be the landing page of the site as it flips through posts. So does that mean we should avoid using the above and stick to a custom function (I've currently got one created in functions.php)?
    – CTully12
    Commented Jan 15, 2014 at 21:16
  • This was what i am searching, thanks a lot, you saved my day Cheers !!
    – Ujjwal
    Commented Jul 31, 2015 at 7:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.