Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question
add comment

4 Answers 4

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.

share|improve this answer
add comment
<?php get_previous_posts_link(); ?>

Obviously you store this in an array, like

<?php $prev = get_previous_posts_link(); ?>
share|improve this answer
    
Is there a way to just get the link, and not the <a href=""></a> element? –  Ben Everard Jul 8 '12 at 18:41
add comment

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];
?>
share|improve this answer
add comment

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.

share|improve this answer
    
A GUID is not a link. It looks like one but it is not. –  toscho Jul 8 '12 at 20:13
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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