Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Last active January 2, 2016 17:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tommcfarlin/4b04d1e8aac0898b91ac to your computer and use it in GitHub Desktop.
Save tommcfarlin/4b04d1e8aac0898b91ac to your computer and use it in GitHub Desktop.
[WordPress] Adding attributes to single post pagination links.
<?php
/**
* Adds the title attribute to the 'Next and 'Previous' post pagination anchors.
*
* @param string $link The anchor element for the next or previous post.
* @return string The pagination link with the additional attribute.
*/
function acme_add_title_to_single_post_pagination( $link ) {
if ( 0 < strpos( $link, 'rel="prev"' ) ) {
$previous_post = get_previous_post();
$link = str_replace( 'rel="prev"', 'rel="prev" title="' . esc_attr( get_the_title( $previous_post->ID ) ) . '"', $link );
} else {
$next_post = get_next_post();
$link = str_replace( 'rel="next"', 'rel="next" title="' . esc_attr( get_the_title( $next_post->ID ) ) . '"', $link );
} // end if/else
return $link;
} // end acme_add_title_to_single_post_pagination
add_filter( 'next_post_link', 'acme_add_title_to_single_post_pagination' );
add_filter( 'previous_post_link', 'acme_add_title_to_single_post_pagination' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment