My Site Address (URL) is . I have a header Custom Links menu URL like this "/#services". This takes me to a location on the current page but I want it to take me to the location on a root page. Is there a variable I can use that represents Site Address (URL) or WordPress Address (URL), e.g. "[site_url]/#services"?
I need this because I want to be able to change my site's URL without going through all the menus and changing the URL. I want menus to reflect the site root from the settings of the Wordpress.
Related question: When I click the link for the first time it does not jump to #services on the page, why do I need to click on it twice in the row?
Edited: Why would anyone want links to be relative to the root and not to the Site Address (URL)?
My Site Address (URL) is http://something.ddns/PS. I have a header Custom Links menu URL like this "/#services". This takes me to a location on the current page but I want it to take me to the location on a root page. Is there a variable I can use that represents Site Address (URL) or WordPress Address (URL), e.g. "[site_url]/#services"?
I need this because I want to be able to change my site's URL without going through all the menus and changing the URL. I want menus to reflect the site root from the settings of the Wordpress.
Related question: When I click the link for the first time it does not jump to #services on the page, why do I need to click on it twice in the row?
Edited: Why would anyone want links to be relative to the root and not to the Site Address (URL)?
Share Improve this question edited Jan 31, 2020 at 22:31 Hrvoje Batrnek asked Jan 31, 2020 at 5:30 Hrvoje BatrnekHrvoje Batrnek 1134 bronze badges1 Answer
Reset to default 1If you have Custom Link URL exactly as written in your example, it should work as expected. "/" at the beginning already means "site root", and it should work on single (first) click. Maybe you have some menu related plugin installed, or your theme changes menu URLs in some way. Or there is client side javascript involved.
What URL do you see in browser status bar (lower left corner) when you hover over custom menu link? It should be [site_root]/#services
.
UPDATE:
When WordPress is installed in subdirectory /#services
will link to incorrect page. There is no way to add [site_root]
prefix in menu URLs, so you either use have to use real URL /PS/#services
(and will have to change them all after changing site root) or you can add filter to automatically prefix all menu items starting with /
. Just keep in mind, that in case you need to add link to real site root, you must use full URL http://something.ddns/
instead of /
.
function my_nav_menu_link_attributes( $atts ) {
if ( '/' == substr( $atts['href'], 0, 1 ) ) {
$atts['href'] = site_url() . $atts['href'];
}
return $atts;
}
add_filter( 'nav_menu_link_attributes', 'my_nav_menu_link_attributes', 20 );