8

I currently use the following method to get a prev / next blog page link:

<?php previous_post_link( '%link', '%title' ); ?>

However this includes the outer element, name etc, I want just the link e.g. http://domain.com/blog/page/2

Is there a function that I'm not seeing to suit my requirements?

Clarification

  • This is for the next blog page (not blog post)
  • I only want the link, not the anchor element

Many thanks

0

6 Answers 6

19

Short answer: get_previous_posts_page_link is the function you want:

<?php if($url = get_previous_posts_page_link()): ?>
  do stuff with $url
<?php endif; ?>

Longer answer: follow the code.

previous_posts_link calls get_previous_posts_link.

<?php
/**
 * Display the previous posts page link.
 *
 * @since 0.71
 * @uses get_previous_posts_link()
 *
 * @param string $label Optional. Previous page link text.
 */
function previous_posts_link( $label = null ) {
    echo get_previous_posts_link( $label );
}

get_previous_posts_link calls previous_posts to fetch the link URI itself.

<?php
/**
 * Return the previous posts page link.
 *
 * @since 2.7.0
 *
 * @param string $label Optional. Previous page link text.
 * @return string|null
 */
function get_previous_posts_link( $label = null ) {
    global $paged;

    if ( null === $label )
        $label = __( '&laquo; Previous Page' );

    if ( !is_single() && $paged > 1 ) {
        $attr = apply_filters( 'previous_posts_link_attributes', '' );
        return '<a href="' . previous_posts( false ) . "\" $attr>". preg_replace( '/&([^#])(?![a-z]{1,8};)/i', '&#038;$1', $label ) .'</a>';
    }
}

previous_posts is just a little wrapper around the function you're looking for: get_previous_posts_page_link

<?php
/**
 * Display or return the previous posts page link.
 *
 * @since 0.71
 *
 * @param boolean $echo Optional. Echo or return;
 */
function previous_posts( $echo = true ) {
    $output = esc_url( get_previous_posts_page_link() );

    if ( $echo )
        echo $output;
    else
        return $output;
}

The function we want:

<?php
/**
 * Retrieve previous posts page link.
 *
 * Will only return string, if not on a single page or post.
 *
 * Backported to 2.0.10 from 2.1.3.
 *
 * @since 2.0.10
 *
 * @return string|null
 */
function get_previous_posts_page_link() {
    global $paged;

    if ( !is_single() ) {
        $nextpage = intval($paged) - 1;
        if ( $nextpage < 1 )
            $nextpage = 1;
        return get_pagenum_link($nextpage);
    }
}

I included all of that to illustrate how you can go about finding answers by looking through the core. Tools like ack can help you get started:

shell$ cd /path/to/your/wordpress/install
shell$ ack "function previous_posts_link"

In general, the WordPress is pretty good at making sure a function only does one thing. Tracing from broader functions (ala previous_posts_link and other template tags) back to more basic functions is usually a good way to learn some cool stuff and find your answer.

1
  • Wow, very informative answer, and does the job 5 years on, thank you!
    – Davey
    Commented Jul 4, 2017 at 16:40
1
<?php get_previous_posts_link(); ?>

Obviously you store this in an array, like

<?php $prev = get_previous_posts_link(); ?>
1
  • 2
    Is there a way to just get the link, and not the <a href=""></a> element? Commented Jul 8, 2012 at 18:41
0

If you don't want any other elements then you can add the following code to your theme's page template instead. You links will then be assigned to $prevID and $nextID which you can do what you like with.

<?php
$pagelist = get_pages('sort_column=menu_order&sort_order=asc');
$pages = array();
foreach ($pagelist as $page) {
$pages[] += $page->ID;
}

$current = array_search($post->ID, $pages);
$prevID = $pages[$current-1];
$nextID = $pages[$current+1];
?>
0

Similar to @liam-jay, but only getting one of the posts

$prev_post = get_previous_post();
empty($prev_post) or $link = $prev_post->guid;

I think this should work fine, untested though.

1
  • A GUID is not a link. It looks like one but it is not.
    – fuxia
    Commented Jul 8, 2012 at 20:13
0

this is working:

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

For posterity. previous_posts() and next_posts() will do just that.

echo previous_posts( 0, false );
echo next_posts( 0, false );

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.