If I understand the problem setup correctly, you could try to do the break and widget class counting within the wp_nav_menu_objects
filter.
Here’s an updated example, it’s rather expanded because of the extra debug part:
add_filter( 'wp_nav_menu_objects',
function( $items, $args ) {
// Only apply this for the 'primary' menu:
if( 'primary' !== $args->theme_location )
return $items;
// Here "x_" means the latest root li (depth 0)
static $x_pid = 0; // post ID of the latest root li (depth 0)
static $x_key = 0; // array key of the latest root li (depth 0)
static $x_cols = 0; // n breaks or widgets gives n+1 columns
static $x_has_dropdown = false; // if the latest root li (depth 0) has dropdown
// Internals:
$tmp = array();
$debug_string = '';
$show_debug = true; // Edit this to your needs:
foreach( $items as $key => $item )
{
// Debug:
$debug = array();
$debug['ID'] = $item->ID;
$debug['title'] = $item->title;
$debug['key'] = $key;
$debug['x_key'] = $x_key;
$debug['depth'] = '';
$debug['menu_item_parent'] = $item->menu_item_parent;
$debug['has_widget_class'] = 0;
$debug['is_depth_1_first_child'] = 0;
$debug['x_has_dropdown'] = 0;
$debug['has_break_class'] = 0;
$debug['x_cols_increase'] = 0;
// Collect columns increaments:
$inc = 0;
// Depth 0:
if( 0 == $item->menu_item_parent )
{
$debug['depth'] = 0;
// Resets:
$x_key = $key;
$x_pid = $item->ID;
$x_cols = 0;
$x_has_dropdown = false;
// If widget class exists:
if( in_array( 'widget', $item->classes, 1 ) )
{
$debug['has_widget_class'] = '1';
$inc++;
}
}
// Depth 1:
if( $x_pid == $item->menu_item_parent )
{
$debug['depth'] = 1;
// Increase the columns count for an existing dropdown:
if( ! $x_has_dropdown )
{
$inc++;
$x_has_dropdown = true;
}
// Check for the 'break' class:
if( in_array( 'break', $item->classes, 1 ) )
{
$debug['x_has_break_class'] = 1;
// First li child:
if( $x_key+1 == $key+0 )
{
$debug['is_depth_1_first_child'] = 1;
}
else
{
$debug['is_depth_1_first_child'] = 0;
$inc++;
}
}
$t[$x_key] = $x_cols;
}
$debug['x_has_dropdown'] = (int) $x_has_dropdown;
// Increase the columns count:
$debug['x_cols_increase'] = $inc;
$x_cols += $inc;
$debug['x_cols'] = $x_cols;
// Collect the debug:
$debug_string .= print_r( $debug, 1 );
} // end foreach
// Show debug info:
if( $show_debug )
printf( "<!-- debug: %s -->", $debug_string );
// Insert the new 'mega menu' class to the corresponding menu object:
foreach( $t as $key => $value )
{
$items[$key]->classes[] = sprintf( 'mega-menu-columns-%d', $value );
}
return $items;
}
, PHP_INT_MAX, 2 );
With your current menu tree, I get this debug information:
<!-- debug: Array
(
[ID] => 3316
Mega Menu Walker => About Us
[key] => 1
[x_key] => 0
[depth] => 0
[menu_item_parent] => 0
[has_widget_class] => 1
[is_depth_1_first_child] => 0
[x_has_dropdown] => 0
[has_break_class] => 0
[x_cols_increase] => 1
[x_cols] => 1
)
Array
(
[ID] => 3317
Mega Menu Walker => Company Profile
[key] => 2
[x_key] => 1
[depth] => 1
[menu_item_parent] => 3316
[has_widget_class] => 0
[is_depth_1_first_child] => 1
[x_has_dropdown] => 1
[has_break_class] => 0
[x_cols_increase] => 1
[x_has_break_class] => 1
[x_cols] => 2
)
Array
(
[ID] => 3318
Mega Menu Walker => Our Team
[key] => 3
[x_key] => 1
[depth] =>
[menu_item_parent] => 3317
[has_widget_class] => 0
[is_depth_1_first_child] => 0
[x_has_dropdown] => 1
[has_break_class] => 0
[x_cols_increase] => 0
[x_cols] => 2
)
Array
(
[ID] => 3319
Mega Menu Walker => Leadership Team
[key] => 4
[x_key] => 1
[depth] => 1
[menu_item_parent] => 3316
[has_widget_class] => 0
[is_depth_1_first_child] => 0
[x_has_dropdown] => 1
[has_break_class] => 0
[x_cols_increase] => 0
[x_cols] => 2
)
Array
(
[ID] => 3320
Mega Menu Walker => Professional Affiliations
[key] => 5
[x_key] => 1
[depth] => 1
[menu_item_parent] => 3316
[has_widget_class] => 0
[is_depth_1_first_child] => 0
[x_has_dropdown] => 1
[has_break_class] => 0
[x_cols_increase] => 0
[x_cols] => 2
)
Array
(
[ID] => 3321
Mega Menu Walker => Clients
[key] => 6
[x_key] => 1
[depth] => 1
[menu_item_parent] => 3316
[has_widget_class] => 0
[is_depth_1_first_child] => 0
[x_has_dropdown] => 1
[has_break_class] => 0
[x_cols_increase] => 1
[x_has_break_class] => 1
[x_cols] => 3
)
Array
(
[ID] => 3322
Mega Menu Walker => Partnerships
[key] => 7
[x_key] => 1
[depth] => 1
[menu_item_parent] => 3316
[has_widget_class] => 0
[is_depth_1_first_child] => 0
[x_has_dropdown] => 1
[has_break_class] => 0
[x_cols_increase] => 0
[x_cols] => 3
)
-->
If you want to check if the walker object is of the megaMenuWalker
class, you can use:
if( ! is_object( $args->walker ) || ! is_a( $args->walker, 'megaMenuWalker' ) )
return $items;
instead of
if( 'primary' !== $args->theme_location )
return $items;
I hope this helps.