Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

How to add url prefix to a secondary menu? WordPress

  • SOLVED

I'm working on a site that has two separate menus that should have been combined into one. I don't have the time to completely rebuild these so I was wondering if there is a way to add a url prefix to the secondary menu, so the url of the pages on this menu would be like this www.my-site.com/prefix/page-name, instead of www.my-site.com/page-name. These pages also share a unique template file.

If you need further clarification, please let me know.

Thanks!

Answers (3)

2011-07-13

John Cotton answers:

Hi Jeremy

Add this to your functions.php file:

function modifiy_nav_menu_items($items, $args) {

if( $args->theme_location == 'Marathon' ) {
foreach( $items as $item ) {
// change $item->url as you need to;
}
}

return $items;
}
add_action( 'wp_nav_menu_objects', 'modifiy_nav_menu_items', 10, 2 );


In the item loop you can modify the url value (it's just a string) however you want and this new value will be used in the output.

JC


Jeremy Phillips comments:

Thanks, I tested it but I'm not quite clear on how I should customize it for my needs. I want the marathon menu items to have 'marathon' in their url, so they appear like this:
www.my-site.com/marathon/page-name


John Cotton comments:

From what you said in your original post, something as simple as this would work:


$item->url = str_replace( 'my-site.com/', 'my-site.com/marathon/', $item->url);


but obviously you can add in whatever logic you want there...


Jeremy Phillips comments:

Is this the way that it should be implemented? This didn't seem to work.

function modifiy_nav_menu_items($items, $args) {
if( $args->theme_location == 'Marathon' ) {
foreach( $items as $item ) {
$item->url = str_replace( 'mysite.com/', 'mysite.com/marathon/', $item->url);
// change $item->url as you need to;
}
}
return $items;
}
add_action( 'wp_nav_menu_objects', 'modifiy_nav_menu_items', 10, 2 );


Thanks!


John Cotton comments:

In what way didn't it work?


Jeremy Phillips comments:

I inserted the above code into my functions.php and tested the Marathon menu. Pages from that menu still lacked /marathon from their url.


John Cotton comments:

Apologies if this sounds daft, but can I take it that you replaced the "my-site" with the real site name?


Jeremy Phillips comments:

Yeah, and I tried with and without http:// on a couple sites no no avail.


John Cotton comments:

Well, there's three possibilities:

1/ The function is never being executed - you could pop a little echo in at the top to see whether it's being called
2/ The $args->theme_location isn't called 'Marathon' - have you tried lower case? It needs to precisely match whatever you declared when the menu was registered
3/ The urls you have in items don't have whatever you're looking for in them

So I would mod the code to this and see what happens:



function modifiy_nav_menu_items($items, $args) {
echo "modifiy_nav_menu_items called<br/>\n";
echo "location = ". $args->theme_location . '<br/>';

if( $args->theme_location == 'Marathon' ) {

foreach( $items as $item ) {
echo $item->url. '<br/>';
$item->url = str_replace( 'mysite.com/', 'mysite.com/marathon/', $item->url);

// change $item->url as you need to;

}

}

return $items;

}

add_action( 'wp_nav_menu_objects', 'modifiy_nav_menu_items', 10, 2 );


Jeremy Phillips comments:

The echo from the above code worked, although still no change to the url. I've been trying lowercase as well as uppercase.

Not sure I understand what you mean by this:
<em>3/ The urls you have in items don't have whatever you're looking for in them</em>


John Cotton comments:

You're not giving me enough info - can you send me a link to the site?

2011-07-15

Svilen Popov answers:

Try the [[LINK href="http://wordpress.org/extend/plugins/custom-permalinks/"]]<strong>Custom Permalinks</strong>[[/LINK]].

2011-07-12

Dylan Kuhn answers:

I'm not too experienced with the nav menus, but could it be as simple as creating a page named prefix and making it the parent of the secondary pages?


Jeremy Phillips comments:

Hmm. That could work, only I don't want the parent 'prefix' page showing up in the menu.


Dylan Kuhn comments:

I guess the menu must be generated with wp_list_pages()? Might be useful to see the code, but I've used this plugin to remove pages from those lists: [[LINK href="http://wordpress.org/extend/plugins/page-lists-plus/"]]http://wordpress.org/extend/plugins/page-lists-plus/[[/LINK]]


Jeremy Phillips comments:

Here is my code:

<?php wp_nav_menu(
array( 'menu' => 'Marathon',
'container' => 'div',
'container_class' => false,
'container_id' => 'blue-nav',
'before' => '<div>',
'after' => '<div></div></div>',
'walker' => new slugs_as_ids()
)
);
?>


I'll take a look at that plugin.


Dylan Kuhn comments:

Ah, you're not using wp_list_pages(), so don't bother with page-lists-plus. Can you just edit the 'Marathon' menu to contain only the pages you want?