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.

For some reason a title attribute is not appended to next_post_link and prev_post_link calls in WordPress. How can I add one?

share|improve this question
    
There's a next|previous_post_link filter. One way is to use regex. btw, paste your current prev/next code. –  zeo Mar 25 '11 at 21:05

5 Answers 5

up vote 3 down vote accepted

Update

As I deleted the Repo on GitHub, here's a new answer.

add_filter( 'previous_post_link', 'wpse13044_adjacent_post_link_tooltip', 10, 2 );
add_filter( 'next_post_link', 'wpse13044_adjacent_post_link_tooltip', 10, 2 );
function wpse13044_adjacent_post_link_tooltip( $format, $link )
{
    $previous = 'previous_post_link' === current_filter();
    // Get the next/previous post object
    $post = get_adjacent_post(
         false
        ,''
        ,$previous
    );
    // Copypasta from cores `get_adjacent_post_link()` fn
    '' === $title = get_the_title( $post->ID );
        AND $title = $previous 
            ? sprint( __( 'Previous Post: %s', 'your_textdomain' ), $title )
            : sprint( __( 'Next Post: %s', 'your_textdomain' ), $title );

    $format = str_replace(
         'rel="'
        ,sprintf(
             'title="%s" rel="'
            ,$title
         )
        ,$format
    );

    return "<span class='some classes'>{$format}</span>";
}
share|improve this answer
    
I would much rather avoid a plugin if at all possible! –  Infocentre Mar 25 '11 at 12:29
    
@Infocentre: It's not meant to be a plugin. I wrote it for all the answers about pagination. It's over documented and shows you everything you might want to know about pagination and all possibilities. So take a look (especially at the line number i told you) and see how to do it. –  kaiser Mar 25 '11 at 12:53
    
excuse me for being a little dumb, but i am unsure what to do with this info? –  Infocentre Mar 25 '11 at 14:17
    
Look into the plugin starting at line 155 with <li class="page-num page-num-prev"> and how i rebuild the prev_post_link() function. This shows you a possiblity how you can modify and build the links the way you want to have it. (Mostly copy&paste). –  kaiser Mar 25 '11 at 14:32
1  
@LeaCohen I deleted that repo. Added a replacement. –  kaiser Feb 19 '13 at 8:08

I'm trying to do this right now as well. The filter function seems like the best bet.

This is where I'm at now, but I can't seem to get the title of the next or previous post and pass it to the filter.

Edit: Figured it out. A bit hackey probably, but it works.

add_filter('next_post_link','add_title_to_next_post_link');
function add_title_to_next_post_link($link) {
global $post;
$post = get_post($post_id);
$next_post = get_next_post();
$title = $next_post->post_title;
$link = str_replace("rel=", " title='".$title."' rel", $link);
return $link;
}

add_filter('previous_post_link','add_title_to_previous_post_link');
function add_title_to_previous_post_link($link) {
global $post;
$post = get_post($post_id);
$previous_post = get_previous_post();
$title = $previous_post->post_title;
$link = str_replace("rel=", " title='".$title."' rel", $link);
return $link;
}
share|improve this answer

A bit old perhaps, and I wasn't really sure on how to make a comment on a reply...

In short, after looking for the same solution I've modified Picard102's suggestion just a tiny bit:

/**
 * Filter previous_post_link and next_post_link
 */
function filter_next_post_link($link) {
    global $post;
    $post = get_post($post_id);
    $next_post = get_next_post();
    $title = $next_post->post_title;
    $link = str_replace("rel=", 'title="' . $title . '" rel=', $link);
    return $link;
}
add_filter('next_post_link', 'filter_next_post_link');

function filter_previous_post_link($link) {
    global $post;
    $post = get_post($post_id);
    $previous_post = get_previous_post();
    $title = $previous_post->post_title;
    $link = str_replace("rel=", 'title="' . $title . '" rel=', $link);
    return $link;
}
add_filter('previous_post_link', 'filter_previous_post_link');
share|improve this answer

No need for functions and filters all you need to do is to use get_adjacent_post instead of next_post_link and prev_post_link, Note that get_adjacent_post is used to get previous and next post, you can read about it here To get previous post and it's title attribute use this

$prev_post = get_adjacent_post(false, '', true);
if(!empty($prev_post)) {
echo '<a href="' . get_permalink($prev_post->ID) . '" title="' . $prev_post->post_title . '">' . $prev_post->post_title . '</a>'; }

To get next post and it's title attribute use this

$next_post = get_adjacent_post(false, '', false);
if(!empty($next_post)) {
echo '<a href="' . get_permalink($next_post->ID) . '" title="' . $next_post->post_title . '">' . $next_post->post_title . '</a>'; }
share|improve this answer

you could use this plugin: post link plus. Has many additional configuration options that can come in handy for post navigation.

share|improve this answer

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.