WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is it possible to add a class or ID to next/prev image links in attachment.php?

I tried like this but it didn't work:

<?php next_image_link( false, 'Next', array('class' => 'next')); ?>
share|improve this question
Any progress? Was my answer useful? – toscho Jan 2 '13 at 17:21
yup 100% working, thanks – wp student Feb 1 '13 at 13:31
up vote 1 down vote accepted

There two hooks to filter these links: previous_image_link and next_image_link. So we can add these classes per filter:

add_filter( 'previous_image_link', 'wpse_77296_img_link_class' );
add_filter( 'next_image_link',     'wpse_77296_img_link_class' );

 * Add CSS class to image navigation links.
 * @wp-hook previous_image_link
 * @wp-hook next_image_link
 * @param   string $link Complete markup
 * @return  string
function wpse_77296_img_link_class( $link )
    $class = 'next_image_link' === current_filter() ? 'next' : 'prev';

    return str_replace( '<a ', "<a class='$class'", $link );
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.