Last active
January 2, 2016 17:09
-
-
Save tommcfarlin/4b04d1e8aac0898b91ac to your computer and use it in GitHub Desktop.
[WordPress] Adding attributes to single post pagination links.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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