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?
-
1You could use javascript. – Cthulhu Oct 22 '12 at 15:05
-
I'm not a programmer so I would prefer if there's a plugin – Ruriko Oct 22 '12 at 23:40
-
1Plugin recommendation is off-topic now... & there are plenty of Imagebox plugins in the Repository, surely one or more will have this feature. – brasofilo Oct 26 '12 at 18:21
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".
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"><< Previous Image</div>
</a>
<a href="http://coolwebsite.com/attachment/05/">
<div class="next-image">Next image >></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.
-
1If the user is on an attachment page then there will be a class on the body tag to show that, so you could just update your jQuery selector accordingly and your code will then only apply in the right context. To make sure dependencies load in the right order, consider adding your code using
wp_enqueue_script
instead. Then you can also add a conditional usingis_attachment
and so only load your script on attachment pages. – Andy Macaulay-Brook Jul 21 '16 at 12:13