Archived topic
Back-to-Top anchor link removal
9 replies · Started by John on June 25, 2022
Hi,
This seems kind of minor, but I was wondering if there is a way to prevent '/#top' from being appended to the page URL after clicking on the Back-to-Top button.
I have some sites which do not append the /#top to the URL. My other GeneratePress / GenerateBlocks websites do append the /#top. Some do. I compared the two websites and cannot see the difference in the customizer settings between the two sites. Links below:
https://emersonhealthcare.com/ - does not append
https://fkarch.com/ - does not append
https://carlsonwebdesign.com/ - appends
https://www.adamdaviesexplorer.com/ - appends
I have no idea what settings are different between these two websites that would affect this issue. Any help you can provide is appreciated!
Thanks,
John Carlson
Hi John,
I was wondering if there is a way to prevent ‘/#top’ from being appended to the page URL after clicking on the Back-to-Top button.
That is not something GP adds so not normal behavior.
I have some sites which do not append the /#top to the URL.
That is normal behavior.
Have you tried #1 here?
https://docs.generatepress.com/article/debugging-tips/
Maybe some difference in my child themes functions.php? I'll look into it and try the debug. Thanks, Leo.
Hey Leo - It's definitely something in my functions.php file that is causing #top to be appended. I'll track down the line of code that is causing the issues and remove it. Thanks for your quick reply!
John
No problem :)
Leo,
FYI - Here is the code in my functions file that was causing the issue:
// Apply smooth scroll to all hash links
add_filter( 'generate_smooth_scroll_elements', function( $elements ) {
$elements[] = 'a[href*="#"]';
return $elements;
} );
No problem :)
We can exclude the back-to-top button if you still need that piece of code on your site.
No, that's okay. That line of code just found its way into the functions.php in my default child theme because I had used it on another website.
I think I had originally wanted it because I wanted to smooth-scroll from the "Leave a comment" link down to the comment area. Do you know of another way to do that which doesn't trigger the #top issue?
This should work:
add_filter( 'generate_smooth_scroll_elements', function( $elements ) {
$elements[] = 'a[href*="#"]:not[generate-back-to-top]';
return $elements;
} );
Hi Leo,
No, sorry, that code does not work for me. I'll try to figure it out.
Thanks for your assistance.