Navigate through the posts using keyboard arrows
-
I’m building my site on localhost and I’d like to navigate to next and previous posts by using keyboard right and left arrow keys.
I know about the WP functions
get_adjacent_post
,get_next_post
andget_previous_post
, but since I’m new to this I don’t know how to use them.I found this code and placed it in the header of the site
<script type="text/javascript"> document.onkeydown = function (e) { var e = e || event, keycode = e.which || e.keyCode; if (keycode == 37 || keycode == 33) location = "<?php echo get_permalink(get_previous_post()); ?>"; if (keycode == 39 || keycode == 34) location = "<?php echo get_permalink(get_next_post()); ?>"; } </script>
the good thing is that by pressing the left/right arrows something happen, the bad thing is that the previous/next posts are not loaded, something like
https://localhost/testsite/%3C?php%20echo%20get_permalink(get_next_post());%20?%3E
is loaded instead.How could this be fixed?
Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
- The topic ‘Navigate through the posts using keyboard arrows’ is closed to new replies.