Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[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
You can’t perform that action at this time.