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 am trying to add menu items dynamically using the wp_nav_menu_items filter. What I am doing is working fine on two of my menus but I cannot make it work for the third.

Code to register the menus:

register_nav_menus( array(
        'primary' => __( 'Primary Menu', 'mytheme' ),
        'top' => __( 'Top Menu', 'mytheme' ),
        'top2' => __( 'Top Menu 2', 'mytheme' )
    ) );

Code to display the primary menu (this is the one I cannot get things to work)

$args = array(
    'menu' => 'primary',
    'theme_location'  => 'primary'

Code to display the top menu (this one works and new items are added)

$args = array(
    'menu' => 'top',
    'theme_location'  => 'top'

Code to add new items:

add_filter( 'wp_nav_menu_items', 'fx_user_menu', 10, 2 );
function fx_user_menu( $items, $args ) {
    if ( $args->theme_location == 'top')  {
        $items .= '<li>Top </li>';
    if ($args->theme_location == 'top2') {
        $items .= '<li>Top2</li>';
    if ($args->theme_location == 'primary') {
        $items .= '<li>Primary</li>';
    return $items;

Any idea on why I cant add new items to the primary menu? I have done a var_dump($args) which returns nothing for the primary. Top and Top2 return $args as expected.

share|improve this question

Your Answer


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

Browse other questions tagged or ask your own question.