Wordpress - How to add CSS Class to previous_post_link or get previous/next post link URL
There are filters for previous_post_link and next_post_link functions which works in a different way from previous_posts_link_attributes and next_posts_link_attributes, I am not sure why this is not documented on the wordpress website.
function posts_link_next_class($format){
$format = str_replace('href=', 'class="next clean-gray" href=', $format);
return $format;
}
add_filter('next_post_link', 'posts_link_next_class');
function posts_link_prev_class($format) {
$format = str_replace('href=', 'class="prev clean-gray" href=', $format);
return $format;
}
add_filter('previous_post_link', 'posts_link_prev_class');
You can use the more native function that is "below" the previous_/next_post_link();
:
# get_adjacent_post( $in_same_cat = false, $excluded_categories = '', $previous = true )
$next_post_obj = get_adjacent_post( '', '', false );
$next_post_ID = isset( $next_post_obj->ID ) ? $next_post_obj->ID : '';
$next_post_link = get_permalink( $next_post_ID );
$next_post_title = '»'; // equals "»"
?>
<a href="<?php echo $next_post_link; ?>" rel="next" class="pagination pagination-link pagination-next">
<?php echo $next_post_title; ?>
</a>
You could put an element around the function call and style it that way. Like this:
<div class="previous_post_link"><?php previous_post_link('%link'); ?></div>
then control the link in the css.
.previous_post_link a { some styles here }