I'm creating a menu with the wp_nav_menu() function. Now I'd like to add a GET variable to the end of ever link in the menu, like this: www.mysite.com/page/?variable=123

Is there any way I can do this when using wp_nav_menu?

Thanks for your suggestions


Filter 'wp_nav_menu_objects'. You get an array as argument, a list of all items.

Pseudo-code, not tested:

add_filter( 'wp_nav_menu_objects', 'wpse_76401_filter' );

function wpse_76401_filter( $items )
    $out = array();
    foreach ( $items as $item )
        if ( isset ( $item->url ) )
            $item->url = add_query_arg( 'variable', '123', $item->url )

        $out[] = $item;

    return $out;
| improve this answer | |
  • Tried that and it does exactly what I need – perfect solution. Thanks a lot! – Max Min Dec 17 '12 at 16:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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