0

I am having one issue and I don't know how to handle it. In Wordpress menu I am trying to add custom link menu item that will point to "mysitehomepage/#section-1"

In custom links I know how to point it to #section-1 but when user is on some other page (Blog, Contact or etc.) than that link is not pointing to #section-1 because that section only exist on homepage.

Of course this can be solved by adding mysite url before "/#section-1" but this is going to be a template for Wordpress so site url will be different every time. So I need some solution to get the link of my homepage + #section-1.

I hope I explained it good enough. :) And thanks.

3
  • If you don't mind, You can use scrollTo JQuery function in wordpress footer. This will allow every page to scroll at section-1 Commented Jan 20, 2016 at 12:11
  • You said that it is template. But if you will add cusom link via admin panel it'll be appeared in db. So user, which will install your template, will not have it, because it is only in your db. So better to do it through the code wp_nav_menu hook. Commented Jan 20, 2016 at 12:33
  • As I see it can help me, but I am not so good at coding in wordpress :/ Can you please help me how to use wp_nav_menu hook ? Commented Jan 21, 2016 at 15:49

1 Answer 1

1

Here is the code, that will add new item to the existing menu
Add it to functions.php file

function new_nav_menu_items($items) {
    $homelink = '<li class="home"><a href="' . home_url( '/#section-1' ) . '">' . __('Home') . '</a></li>';
    $items = $homelink . $items;
    return $items;
}
add_filter( 'wp_nav_menu_items', 'new_nav_menu_items' );
Sign up to request clarification or add additional context in comments.

2 Comments

It works well just the only thing because this is going to be the theme for wordpress is that users will need to change the code if they want to change the menu. Is there some way to put this menu item to be shown in Appearance -> Menus ?
And by the way - THAAAAAAANK YOOOU! :)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.