7

how can I get a pagination link URL instead of a made-up anchor link? currently, I used

<?php next_posts_link(); ?>
<?php previous_posts_link(); ?>

for pagination but it returns a, I want to know is there any way to get just the next/prev URL?

1

2 Answers 2

10

If you check out the source, they're both wrappers around *_posts(), which in turn are wrappers for get_*_posts_page_link() (where the wildcard indicates either next or previous).

For example, next_posts() will echo or return the escaped URL, depending on the first argument:

$escaped_url = next_posts( false /* Don't echo */ ); 
next_posts(); // Prints escaped URL

Otherwise you can get the raw URL with get_next_posts_page_link() and do with it as you wish:

 $raw_url = get_next_posts_page_link();

 wp_redirect( $raw_url );

 // or...
 echo esc_url( $raw_url );
-2

There are get_previous_posts_link() and get_next_posts_link(), they should do what you want.

3
  • as doc for these 2 functions mentioned, they return previous functions(next_posts_link, previous_posts_link) instead of echoing them, I'm trying to get just the url, like: http://example.com/page/2
    – Amirmasoud
    Commented Dec 4, 2015 at 16:04
  • The Codex page for next_posts_link tells to use these functions when you need the value returned instead of outputted.
    – flomei
    Commented Dec 4, 2015 at 17:48
  • I understand what you're saying but get_*_posts_page_link() did the trick
    – Amirmasoud
    Commented Dec 4, 2015 at 18:41

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.