Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question

1 Answer 1

up vote 0 down vote accepted

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;
}
share|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

 
discard

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.