Navigate with keyboard in Gallery shortcode

The Underscores (_s) starter theme comes with a keyboard navigation script for this. I haven’t tested this myself though. But the JS file looks like this:

jQuery( document ).ready( function( $ ) {
    $( document ).keydown( function( e ) {
        var url = false;
        if ( e.which == 37 ) {  // Left arrow key code
            url = $( '.previous-image a' ).attr( 'href' );
        }
        else if ( e.which == 39 ) {  // Right arrow key code
            url = $( '.entry-attachment a' ).attr( 'href' );
        }
        if ( url && ( !$( 'textarea, input' ).is( ':focus' ) ) ) {
            window.location = url;
        }
    } );
} );

It also needs some modifications to your attachment theme file to make the links work correctly. My suggestion is that you download the _s theme and take a look in the image.php file.

Basically what you need on the attachment page is a link to the next/prev image inside an element with the class “previous-image”https://wordpress.stackexchange.com/”entry-attachment”.

Leave a Comment