Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Enable keyboard navigation for single posts in WordPress. Source: http://techglimpse.com/create-keyboard-navigation-for-your-website/
<?php
//* Euqueue keyboard navigation script
add_action( 'wp_enqueue_scripts', 'customprefix_add_keyboard_navigation' );
function customprefix_add_keyboard_navigation() {
if ( is_single() ) {
wp_enqueue_script( 'keyboard-navigation', get_bloginfo( 'stylesheet_directory' ) . '/js/keyboard-navigation.js', array( 'jquery' ), '1.0.0' );
}
}
jQuery(document).ready(function() {
jQuery(document).keydown(function(e) {
var url = false;
if (e.which == 37) { // Left arrow key code
url = jQuery('.pagination-previous a').attr('href');
} else if (e.which == 39) { // Right arrow key code
url = jQuery('.pagination-next a').attr('href');
}
if (url) {
window.location = url;
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.