1

I have several Wordpress websites with large images at their tops, using 2013 child theme.

How to add #navbar anchor to all page links, so that the browser scrolls down a bit, when a user clicks a link in the Pages widget?

screenshot

(because otherwise users think that nothing has happened after they click, since they see the same big image again).

In the file wp-includes/widgets/class-wp-widget-pages.php I have found the source code for the WP_Widget_Pages, but I am not sure how to modify it the best way (it should probably happen in my twentythirteen-child theme too).

1 Answer 1

2

You can add a filter to page_link to modify link output:

function wpd_add_fragment_to_pages( $url ) {
    return $url . '#navbar';
}
add_filter( 'page_link', 'wpd_add_fragment_to_pages' );
4
  • Thank you, I have added your code to /wp-content/themes/twentythirteen-child/functions.php and it works Commented Apr 2, 2016 at 17:09
  • 1
    just note that it will apply to all page links output, you could target just the sidebar by adding it just before sidebar output and removing it immediately after.
    – Milo
    Commented Apr 2, 2016 at 17:14
  • 2
    ps: and just in case a fragment is already added from another plugin/theme, we could filter late and check for any existing #. I wish PHP/WP would ship with a complete url builder ;-)
    – birgire
    Commented Apr 2, 2016 at 17:17
  • 1
    I've changed the return statement to check if there is an anchor already: return ( preg_match( '/#\w*$/', $url ) ? $url : $url . '#navbar' ); Commented Apr 6, 2016 at 8:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.