Using wp_nav_menu()
, how can I append a value at end of each URL?
For example, I have the following url: http://www.example.com/
but I have to append a language parameter at the end, so the url should be: http://www.example.com/?lang=$language
Leave a Reply
You must be logged in to post a comment.
You can either use a whole custom Walker – see codex or just a filter call to
walker_nav_menu_start_el
to regenerate link.In case of filter, your code in theme’s functions.php would look like this:
This code is a copy of start_el function of Walker_Nav_Menu class, which you can find in wp-includes/nav-menu-template.php – see it’s code.
I found the solution modifying the behaviour of
wp_nav_menu
with thewp_get_nav_menu_items-filter
. Here’s a somewhat complete example:This modifies every item in the navigation menu. So if you have an external link, it will be changed as well.
Plus, modifying the URL is not as easy as it seems. The URL of an item can be
/blabla?myvalue=5#anchor
which would look like/blabla?myvalue=5#anchor&lang=de&foo=bar
after the modification.Here is an example if someone needs to achieve this with javascript & jQuery:
For more info:
URLSearchParams.append()