6

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?

  • 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
6

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>";
}
  • 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
6

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>'; }
  • 1
    Thank you! Cleanest solution. Weird no body bothers to test this, it works great and it also works in woocommerce -- obviously since products are posts. – Christina Dec 10 '15 at 17:39
  • Yeap! Cleanest and the fastest solution. And very customizable. – BasTaller Feb 28 '17 at 18:53
4

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;
}
1

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');
0

Shortened the code a little.

/*
 * Add "title=" to previous_post_link and next_post_link
 */

add_filter('next_post_link', function($link) {
  $next_post = get_next_post();
  $title = $next_post->post_title;
  $link = str_replace('href=', 'title="'.$title.'" href=', $link);
  return $link;
});

add_filter('previous_post_link', function($link) {
  $previous_post = get_previous_post();
  $title = $previous_post->post_title;
  $link = str_replace('href=', 'title="'.$title.'" href=', $link);
  return $link;
});

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