• Usce

    (@usce)


    Hi everyone,

    I’m creating options in my theme customizer based on number of top level items in currently active menu. I want to create options dynamically, and exclude submenu items from creating options, only options for top level menu items should be created.
    This is the code I wrote:

    $dc_get_menu = wp_get_nav_menu_items('Main menu');
        $counter = 0;
    	foreach($dc_get_menu as $key => $menu_item) {
    		if ($menu_item->menu_item_parent != 0 ) continue;
    		 $counter++;
    		 $wp_customize->add_setting( 'dc_menu_icon_' . $counter, array( 
    		    'default' => ' '
    	     ) );
            $wp_customize->add_control( new WP_Customize_Control( $wp_customize, 'dc_menu_icon_' . $counter, array(
                'label' => 'Menu icon ' . $counter,
                'section' => 'divi_center_menu_icons',
                'settings' => 'dc_menu_icon_' . $counter,
            ) ) );
    		
    	}

    It is actually working. But my questions is I have this part here:

    if ($menu_item->menu_item_parent != 0 )

    I used if from other stackoveflow code snippet and implemented here. As can be seen on reference page here:

    menu_item_parent: The DB ID of the nav_menu_item that is this item’s menu parent, if any. 0 otherwise.

    By definition, here if menu item doesn’t have parent 0 should be returned. However when I use to check if ID is 0, I get submenu items counted. And when I use to check if not 0, I got only top level menu items.

    I like to understand my code, even if I somehow made it working, but this is situation where I don’t completely understand why is it working, and even more important is this the best way to do it, and would appreciate if you provide explanation for it.

    Thank you! ??

Viewing 2 replies - 1 through 2 (of 2 total)
  • Joy

    (@joyously)

    By definition, here if menu item doesn’t have parent 0 should be returned. However when I use to check if ID is 0, I get submenu items counted. And when I use to check if not 0, I got only top level menu items.

    I can understand your code and why it works, but I can’t understand your statement above.
    if ($menu_item->menu_item_parent != 0 ) continue;

    This is not a function call, as it sort of sounds like you think it might be. The menu_item_parent if one of the properties of each menu item. It is set to 0, unless there is a parent item, in which case it is set to the parent’s ID. The if statement skips all the menu items which have a non-zero parent, in other words, there is a parent ID there, so it is a submenu.

    Thread Starter Usce

    (@usce)

    I just see from quote, that I used word return in wrong context, sorry for that, I wasn’t trying to express about it as a function.

    I actually was bit confused by if statement the way it was written (haven’t used continue before), as I sourced that from other code snippet. Now I understand how it works, after bit of research and as you explained me that it skips menu items with non-zero parent.

    Thank you for taking time to clarify this ??

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Counting top level items in menu’ is closed to new replies.