WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I create a single page website and use only anchors (#) on main menu items.

Now, I need to include a second page on the website and keep the same menu.

I'm using roots theme. Then, I'm building menu using:

wp_nav_menu(array('theme_location' => 'primary_navigation')); 

But how to change items url from, for example, #contact to mywebsite.com.br/#contact only on this second page?

share|improve this question
Please show some code. How are you building your links? Manually? Using a custom function? Using wp_nav_menu? – gdaniel Mar 12 '14 at 15:54
First, I'm using roots theme. Then, I'm building menu using wp_nav_menu(array('theme_location' => 'primary_navigation')); – marcelo2605 Mar 12 '14 at 15:56
up vote 2 down vote accepted

In your template you can check whether you're on the front-page or not and then echo out a different menu.

For example:

if(is_front_page() || is_home()){

 wp_nav_menu(array('theme_location' => 'primary_navigation'));


 wp_nav_menu(array('theme_location' => 'secondary_navigation'));


You would also have to register a second menu locations in the functions.php file (if it hasn't been done yet).

To register a new menu location:

function register_my_menus() {

        'secondary-location' => __('Secondary Location'),


add_action( 'init', 'register_my_menus' );

The downside is that you would have to manage two menus in the backend. That could be a problem if the menu changes often, because you would have to update two menus.

Instead, you could filter the wp_nav_menu and alter the url before the menu is printed. For example, this would go in functions.php

function change_menu($items){


  foreach($items as $item){

    $item->url = get_bloginfo("url") . "/" .  $item->url;



  return $items;


add_filter('wp_nav_menu_objects', 'change_menu');

the code above filters the wp_nav_menu_object. It adds the full url if you're not on the front-page of the website. Otherwise it just returns the regular menu. Using this method you would not have to create a second menu in the admin.

share|improve this answer
Thanks. So, using wp_nav_menu I can't add a string to URL? – marcelo2605 Mar 12 '14 at 16:41
I just updated my answer. You can use a filter to modify the url before it's printed. – gdaniel Mar 12 '14 at 16:43
That is so helpful, even for my completely different needs, thank you @gdaniel! – physalis Nov 24 '14 at 22:27

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.