Navigate with keyboard in Gallery shortcode

I am using WordPress Gallery shortcode and I was wondering if there is a way to navigate using the arrow keys? Like if I view an single image and used the right arrow to navigate to the next image in the gallery set. Any idea how to do this?

Related posts

Leave a Reply

2 comments

  1. 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” / “entry-attachment”.

  2. I am using standard WP galleries and have each image link to an attachment page where the image is displayed fullsize. To show PREVIOUS and NEXT links in order for the user to get to the corresponding images I found the functions previous_image_link and next_image_link useful.

    In my attachment.php I have the following lines to make WordPress display previous/next links:

    <div class="nav-links">
                        <?php previous_image_link( false, '<div class="previous-image">' . __( '<< Previous Image', '$text_domain' ) . '</div>' ); ?>
                        <?php next_image_link( false, '<div class="next-image">' . __( 'Next image >>', '$text_domain' ) . '</div>' ); ?>
                        </div>
    

    This results in markup that might look like:

    <div class="nav-links">
    <a href="http://coolwebsite.com/attachment/03/">
        <div class="previous-image">&lt;&lt; Previous Image</div>
    </a>
    <a href="http://coolwebsite.com/attachment/05/">
        <div class="next-image">Next image &gt;&gt;</div>
    </a>
    </div>
    

    My client wanted to be able to use his keyboard to switch back and forth between the images of any gallery, so I added jzatt’s jQuery script and edited it to my footer.php that listens for the KEYDOWN event and, if triggered, grabs the links WP provided and changes the URL of the page.

    This is the jQuery script:

    <script>
            $( document ).ready(function() {
                $( document ).keydown( function( e ) {
                    /* In Galleries you can use LEFT and RIGHT ARROW KEYS to go to the previous/next image */
                    var url = false;
                    if ( e.which == 37 ) {  // Left arrow key code
                        url = $( '.previous-image' ).parent().attr( 'href' );
                    } else if ( e.which == 39 ) {  // Right arrow key code
                        url = $( '.next-image' ).parent().attr( 'href' );
                    }
                    //console.log("URL: "+url);
                    if ( url && ( !$( 'textarea, input' ).is( ':focus' ) ) ) {
                        window.location = url;
                    }
                } );            
            });
        </script>
    

    What I need to add, though, is an IF statement to first check whether the user is indeed on an attachment page.