WordPress ScrollTo Hash

I needed to pass a hash, and scroll to the matching anchor on a new page.

Not as easy as it sounds in WordPress. (Pretty permalinks make it not work.)

After much testing and research, here’s the solution that worked best:

<script>
jQuery( document ).ready(function($) {
   var thehash = window.location.hash.replace('#', '');
   $('body, html').animate({ scrollTop: $('.'+thehash).offset().top -150 }, 1000);
});
</script>

I added the “-150” after the offset to show the header above the anchor.

Leave a Reply

Your email address will not be published. Required fields are marked *

Skip to toolbar