Skip to content

Instantly share code, notes, and snippets.

@BronsonQuick
Created January 30, 2013 15:47
Show Gist options
  • Select an option

  • Save BronsonQuick/4674152 to your computer and use it in GitHub Desktop.

Select an option

Save BronsonQuick/4674152 to your computer and use it in GitHub Desktop.
Add a 'parent' CSS class to WordPress wp_nav_menus
<?php
/* Add a function to add a parent class to WordPress parent menus */
function sennza_add_menu_parent_class( $items ) {
$parents = array();
foreach ( $items as $item ) {
if ( $item->menu_item_parent && $item->menu_item_parent > 0 ) {
$parents[] = $item->menu_item_parent;
}
}
foreach ( $items as $item ) {
if ( in_array( $item->ID, $parents ) ) {
$item->classes[] = 'parent';
}
}
return $items;
}
add_filter( 'wp_nav_menu_objects', 'sennza_add_menu_parent_class' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment