Add class to WP next and previous post link
Pretty basic question…
next_post_link() displays a link to the next post. It’s first paramter allows you to wrap the link in markup, like this:
next_post_link('%link');
But how do I actually add a class to the link/anchor itself? Here’s the solution:
Code to be placed in theme functions.php
add_filter('next_post_link', 'post_link_attributes');
add_filter('previous_post_link', 'post_link_attributes');
function post_link_attributes($output) {
$injection = 'class="my-class"';
return str_replace('<a href=', '<a '.$injection.' href=', $output);
}





1 Comment. Leave new
Thanks sir.. your code work like a charm 😀 thank you so much.. thank you