Title: Scroll posts using the keyboard?
Last modified: August 30, 2016

---

# Scroll posts using the keyboard?

 *  Resolved [anth0ny](https://wordpress.org/support/users/anth0ny/)
 * (@anth0ny)
 * [10 years, 6 months ago](https://wordpress.org/support/topic/scroll-posts-using-the-keyboard/)
 * Is it possible in WordPress (or via a plugin) to give visitors the ability to
   scroll through your posts using the keyboard? I load a lot of posts on a page
   at a time and would like to have the following happen:
 * User presses “N” and they automatically scroll to the next post, users press “
   P” and they automatically scroll back to the previous post.
 * Any help would be appreciated.

Viewing 11 replies - 1 through 11 (of 11 total)

 *  [CrouchingBruin](https://wordpress.org/support/users/crouchingbruin/)
 * (@crouchingbruin)
 * [10 years, 6 months ago](https://wordpress.org/support/topic/scroll-posts-using-the-keyboard/#post-6772075)
 * Can you please provide a link to one of your posts? It would be helpful to see
   the ID or class associated with the nav links. Thanks.
 *  [CrouchingBruin](https://wordpress.org/support/users/crouchingbruin/)
 * (@crouchingbruin)
 * [10 years, 6 months ago](https://wordpress.org/support/topic/scroll-posts-using-the-keyboard/#post-6772076)
 * You will need to add some jQuery to capture the key presses and interpret which
   key has been pressed. First, though, you should install a plugin which will allow
   you to add some script code. I like [Header and Footer](http://wordpress.org/plugins/header-footer/).
 * This is the basic code that will capture the N & P keys & navigate to either 
   the new or previous post:
 *     ```
       <script>
       $(document).ready(function(){
          $("body").keydown(function(event){
             if(event.which == 78)  // N key
             {
                window.location = $(".newer a").attr("href"); //redirects to next post
             }
             else if(event.which == 80) // P key
             {
                window.location = $(".older a").attr("href"); //redirects to prev post
             }
          });
       });
       </script>
       ```
   
 * The selectors for the next and previous links, though, are probably different
   for your site, which is why I asked for a link to one of your posts.
 * Note, however, that this code will intercept _all_ key presses. So if the user
   tries to enter a comment or reply on your post, or tries to enter something in
   a search field, any N or P press will take them to the Next or Previous post 
   and they will be unable to finish entering their comment or search term.
 * It might be better to detect an Alt-N or Alt-P instead:
 *     ```
       <script>
       $(document).ready(function(){
          $("body").keydown(function(event){
             if(event.which == 78 && event.altKey)  // Alt-N key
             {
                window.location = $(".newer a").attr("href"); //redirects to next post
             }
             else if(event.which == 80 && event.altKey) // Alt-P key
             {
                window.location = $(".older a").attr("href"); //redirects to prev post
             }
          });
       });
       </script>
       ```
   
 * You should be able to navigate to the next & previous posts [on my test site](http://sandbox.geefamily.net/2015/07/27/gallery-2/)
   using the Alt-N & Alt-P keys.
 *  Thread Starter [anth0ny](https://wordpress.org/support/users/anth0ny/)
 * (@anth0ny)
 * [10 years, 6 months ago](https://wordpress.org/support/topic/scroll-posts-using-the-keyboard/#post-6772102)
 * Wow thanks. My site is pretty vanilla as I’m only using Twenty Fifteen for my
   theme. If you could tell me what you’re looking for and what I would have to 
   modify in the script in case I switch themes that would be _much_ appreciated.
 * Here’s the link …
 * [http://www.fightword.com/](http://www.fightword.com/)
 *  Thread Starter [anth0ny](https://wordpress.org/support/users/anth0ny/)
 * (@anth0ny)
 * [10 years, 6 months ago](https://wordpress.org/support/topic/scroll-posts-using-the-keyboard/#post-6772168)
 * I tried your code and unfortunately it did not work on my site.
 * I used the plugin that you mentioned it and added it into my header but no luck.
   Any ideas?
 *  [CrouchingBruin](https://wordpress.org/support/users/crouchingbruin/)
 * (@crouchingbruin)
 * [10 years, 6 months ago](https://wordpress.org/support/topic/scroll-posts-using-the-keyboard/#post-6772172)
 * Sorry, for some reason I didn’t get an e-mail notification for your post a couple
   of days ago. What you are looking for are the classes or IDs of the elements 
   which contain the navigation links. You can either do a _view source_ or use 
   a web debugging tool like [Firebug](http://getfirebug.com/) or [Chrome Developer Tools](https://developers.google.com/chrome-developer-tools/).
 * For TwentyFifteeen, in the above code, replace this bit:
 *     ```
       .newer a
       ```
   
 * with this:
 *     ```
       .nav-next a
       ```
   
 * and this:
 *     ```
       .older a
       ```
   
 * with this:
 *     ```
       .nav-previous a
       ```
   
 *  Thread Starter [anth0ny](https://wordpress.org/support/users/anth0ny/)
 * (@anth0ny)
 * [10 years, 6 months ago](https://wordpress.org/support/topic/scroll-posts-using-the-keyboard/#post-6772173)
 * On my site I don’t display the navigation links because the site is just one 
   long page of posts.
 * Would it be possible to scroll via an element like “header” or something similar
   that marks the beginning of each of the posts?
 *  [CrouchingBruin](https://wordpress.org/support/users/crouchingbruin/)
 * (@crouchingbruin)
 * [10 years, 6 months ago](https://wordpress.org/support/topic/scroll-posts-using-the-keyboard/#post-6772175)
 * OK, sorry, I didn’t read the original post carefully enough. I thought you wanted
   to scroll from within individual post pages, not up & down through the posts 
   from the blog page. Let me take a look, it might be possible but a little more
   complicated.
 *  [CrouchingBruin](https://wordpress.org/support/users/crouchingbruin/)
 * (@crouchingbruin)
 * [10 years, 6 months ago](https://wordpress.org/support/topic/scroll-posts-using-the-keyboard/#post-6772181)
 * Wasn’t as bad as I thought I would be. Replace all of the previous code with 
   this:
 *     ```
       <script>
       jQuery(document).ready(function($){
   
       var posts = $(".blog .hentry").toArray();
       var current_post = 0;
       var base_url = window.location + "#";
   
          // This bit of code will advance from the current post to the next or previous post by pressing the N or P keys
          $("body").keydown(function(event){
             if(event.which == 78 && event.altKey)  // Alt-N key
             {
                if (current_post < posts.length - 1)
                {
                   ++current_post;
                   window.location = base_url + $(posts[current_post]).attr("id");
                }
             }
             else if(event.which == 80 && event.altKey) // Alt-P key
             {
                if (current_post > 0)
                {
                   --current_post;
                   window.location = base_url + $(posts[current_post]).attr("id");
                }
             }
          });
   
       });
       </script>
       ```
   
 * This should allow you to scroll up & down through your posts from the home/blog
   page by pressing Alt-N (Next) or Alt-P (Previous).
 *  Thread Starter [anth0ny](https://wordpress.org/support/users/anth0ny/)
 * (@anth0ny)
 * [10 years, 6 months ago](https://wordpress.org/support/topic/scroll-posts-using-the-keyboard/#post-6772191)
 * That worked nicely (though I did switch it to the “up” and “down” key).
 * Is there anyway that it would be possible to get this working with the infinite
   scroll feature on? I may be asking for too much but figured it is worth a shot.
 * Thanks for your help.
 *  [CrouchingBruin](https://wordpress.org/support/users/crouchingbruin/)
 * (@crouchingbruin)
 * [10 years, 6 months ago](https://wordpress.org/support/topic/scroll-posts-using-the-keyboard/#post-6772196)
 * Not really easy to do. You would have to modify the infinite scroll function 
   to refresh the posts array after some posts were retrieved.
 *  Thread Starter [anth0ny](https://wordpress.org/support/users/anth0ny/)
 * (@anth0ny)
 * [10 years, 6 months ago](https://wordpress.org/support/topic/scroll-posts-using-the-keyboard/#post-6772203)
 * No worries then. Thanks for all your help.

Viewing 11 replies - 1 through 11 (of 11 total)

The topic ‘Scroll posts using the keyboard?’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 11 replies
 * 2 participants
 * Last reply from: [anth0ny](https://wordpress.org/support/users/anth0ny/)
 * Last activity: [10 years, 6 months ago](https://wordpress.org/support/topic/scroll-posts-using-the-keyboard/#post-6772203)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
