Join

Apply Anchor Offsets to Prevent Content From Hiding Behind the Header

Don't want to mess with code snippets? Request for this to be a feature of MyListing Pro.

Instructions

  1. Create a new PHP code snippet.
  2. Copy the contents of code snippet below.
  3. Paste the contents into your code snippet.
  4. Review any notes that I’ve provided.
  5. Save and enable the code snippet.
  6. Test.

Snippet

If you’ve ever linked to a particular element on a Post, Page, etc., only to find the content hidden behind your Header, then this code snippet is for you.

This snippet assumes your Header is 89px (MyListing’s default Header size) and applies an offset at least the same height as your Header. If your Header ends up being a different height, however you implement it, you can easily adjust the offset to fit your needs.

Looking at Line 10 of the code snippet, I set it for a 109px offset. Assuming the Header is 89px, that gives me a 20px padding between the Header and content.

<?php

add_action("wp_footer", function () {
    if (!defined("ELEMENTOR_VERSION")) {
        return;
    } ?>
<script>
window.addEventListener('elementor/frontend/init', function() {
elementorFrontend.hooks.addFilter( 'frontend/handlers/menu_anchor/scroll_top_distance', function( scrollTop ) {
return scrollTop - 109;
} );
} );
</script>
<?php
});