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.

It's a pretty simple question really! For some reason a title attribute is not appended to next_post_link and prev_post_link calls in Wordpress.

Anyone got any tips on this?

Thanks

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
add comment

7 Answers

up vote 2 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
show 3 more comments

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
add comment

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
add comment

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
add comment
add_filter('next_posts_link_attributes', 'buckneked_posts_link_next_class');
function buckneked_posts_link_next_class() {
    return 'title="next-paginav"';
} 
add_filter('previous_posts_link_attributes', 'buckneked_posts_link_prev_class');
function buckneked_posts_link_prev_class() {
    return 'title="prev-paginav"';
} 
share|improve this answer
    
Can you add some explanation to the answer? –  Brian Fegter Sep 22 '12 at 0:33
add comment
 add_filter('next_posts_link_attributes', '**function_name_for_next**');

 function **function_name_for_next**() {
     return 'title="next-paginav"';
 } 

 add_filter('previous_posts_link_attributes', '**function_name_for_prev**');

 function **function_name_for_prev**() {
     return 'title="prev-paginav"';
 }

next_posts_link_attributes and previous_posts_link_attributes are the hooks.

The return on the function adds that into the link's attributes... you can do almost anything you want with it.

share|improve this answer
add comment

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
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.