Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@billerickson
Created January 23, 2017 17:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save billerickson/7bedd32df2dfc86b7030f38b0d3134b5 to your computer and use it in GitHub Desktop.
Save billerickson/7bedd32df2dfc86b7030f38b0d3134b5 to your computer and use it in GitHub Desktop.
<?php
function be_equal_sized_menu_items( $classes, $item, $args ) {
// Only run on the header menu
if( ‘top_menu’ !== $args->theme_location )
return $classes;
// Only run on top level menu items
if( $item->menu_item_parent )
return $classes;
// Grab all menu items
// Added lines
$menu_name = ‘top_menu’;
if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $menu_name ] ) ) {
$menu = wp_get_nav_menu_object( $locations[ $menu_name ] );
}
// Changed line
$menu_items = wp_get_nav_menu_items($menu->term_id);
// Remove submenu items
foreach( $menu_items as $i => $menu_item )
if( $menu_item->menu_item_parent )
unset( $menu_items[$i] );
// Count all top level items, and add as a class
$classes[] = ‘count-‘ . count( $menu_items );
return $classes;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment