Enhanced Image Navigation

An awesome code snippet that I just found in Automattic’s _s Theme:

/**
 * Filter in a link to a content ID attribute for the next/previous image links on image attachment pages
 *
 * @since _s 1.0
 */
function _s_enhanced_image_navigation( $url, $id ) {
    if ( ! is_attachment() && ! wp_attachment_is_image( $id ) )
        return $url;

    $image = get_post( $id );
    if ( $image->post_parent && $image->post_parent != $id )
        $url .= '#main';

    return $url;
}
add_filter( 'attachment_link', '_s_enhanced_image_navigation', 10, 2 );

This adds the #main attribute to image attachment links, so that the enduser always sees the attachment instantly and doesn’t have to scroll down. Especially handy in when you click through a post gallery!

Leave a Reply