My site is using a nav menu in my custom theme. I want to add navigation links to anchored links that points to specific sections of the site home page using a Custom Link in the nav menu.

I want to use the equivalent of:

<?php echo get_home_url(); ?>/#xxxx

in the URL field. If I use just the anchored link #xxx as the Custom Link, it will work fine on the home page, but it will not link from another page (as that anchor section is not on that page).

There has to be a clean way to do this. When I insert the php code as well, it appears to work on the home page, but not from another page. I’ve gone through all the other related posts, but none seem to address this issue.

2 Answers
2

If all you want is to go to a specific section of the home page, then the easiest way is to use the relative URL mark / and then use the anchor #xxx.

So the custom link for the menu will be: /#xxx.

Note the difference between #xxx and /#xxx as custom link.

The best thing about this approach is that you’ll not have to bother changing the link after you go live, and additionally, this needs no programming.

Leave a Reply

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