Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

How can I add a CSS Class to the previous_post_link output or just get the URL and create the HTML markup myself

share|improve this question

3 Answers 3

up vote 4 down vote accepted

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; ?>
share|improve this answer
The link to your github source is broken. Could you update it please? –  pixeline Jun 27 '13 at 20:00
@pixeline The plugin isn't available anymore. It was simply too much work to keep it up to date for something I offer for free aside from a dozen plugins. –  kaiser Jun 27 '13 at 22:40

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');
share|improve this answer
This is the best option as it replaces the $format variable before it goes into the echo adjacent_post_link() statement. –  eteich Jun 28 '13 at 16:15

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 } 
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.