Skip to content

Instantly share code, notes, and snippets.

@sgelob
Last active September 22, 2015 10:09
Show Gist options
  • Save sgelob/8b7cfd362e9131dd116b to your computer and use it in GitHub Desktop.
Save sgelob/8b7cfd362e9131dd116b to your computer and use it in GitHub Desktop.
Keyboard navigation on single post between the Next / Previous posts in WordPress
/* Keyboard navigation throu Next/Prev posts
Make sure your previous and next post links have the classes prev and next
Example: <a href="#" class="next">Next Post</a>
<a href="#" class="prev">Previous Post</a>
*/
$(document).ready(function(){
// This "clicks" the link
$(function(){
$('.prev').click(function(){
location.href = $(this).attr('href');
});
$('.next').click(function() {
location.href = $(this).attr('href');
});
});
// This registers the keystroke
$(document).keydown(function(event) {
// Left arrow = previous link
if(event.keyCode==37) {
$('.prev').trigger('click');
}
// Right arrow = next link
if(event.keyCode==39) {
$('.next').trigger('click');
}
});
// This prevents it from firing when typing in search box or comments
$('input, textarea').keydown(function(event){
event.stopPropagation();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment