3

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
  • Can't you just put the full URL of the site into the custom link, with the anchor? Commented Aug 20, 2018 at 16:26
  • see my comment below... Commented Aug 20, 2018 at 16:59

2 Answers 2

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.

3
  • This will only work on the home page. If you are on the About Us page it will not jump to the home page anchor. Commented Aug 20, 2018 at 17:06
  • @user1572121: If you put custom link as #xxx then it'll not work on say the About Page, however, if you add the slash (/) before the anchor, like /#xxx then it'll work everywhere. Because after adding the slash at the beginning, it effectively becomes the home page.
    – Scott
    Commented Aug 20, 2018 at 17:11
  • 1
    OK, added the slash and it appears to work as intended. Thanks! Commented Aug 20, 2018 at 17:20
-1

You can add the /index.php/#my-anchor in your custom links, try it.

Or, you can just put your intire URL in your links.

2
  • It doesn't like the /index.php/#anchor path. I want to avoid using the site's full url since it's currently running on my localhost and a demo install. Using the echo get_home_url works on a hard coded menu, but not so much on this custom menu. Commented Aug 20, 2018 at 16:58
  • 1
    You’re probably going to need to do a search replace on the URL when going live anyway. Commented Aug 20, 2018 at 17:03

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.