X

How to Navigate Next, Previous Adjacent Post with jQuery and Arrow Keys

Snippets by IsItWP

Are you looking for a way to navigate next, previous adjacent post with jQuery and arrow keys? While there’s probably a plugin for this, we have created a quick code snippet that you can use to navigate next, previous adjacent post with jQuery and arrow keys.

If you’re using adjacent post relational links in the head of your WordPress theme, then add this little piece of jQuery. It’ll let people navigate next, previous adjacent post with the left and right arrow keys.

Instructions:

All you have to do is add this code to your theme’s header.php file:

	$(document).keydown(function(e){
		if (e.which == 37) { // left arrow
		   if(!prev) return false;
			window.location.href = $('link[rel=prev]').attr('href');
		}
		if (e.which == 39) { // right arrow
		   if(!next) return false;
			window.location.href = $('link[rel=next]').attr('href');
		}
	});

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly copy / paste code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our other articles on the site like: 23 best WordPress themes for non-profits and how to start a WordPress blog.

Comments   Leave a Reply

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!