Forums
Home / Themes and Templates / Adding class to menu items that have child pages
(@sofian777)
10 years, 9 months ago
Hello, i am quite new into PHP and Themeediting but i want to enhance the Twenty twelve with some addition to menu entries that have a submenu. The theme doesnt add any class but i found a solution here in the codex:
http://codex.wordpress.org/Function_Reference/wp_nav_menu Chapter: How to add a parent class for menu item
But it doesnt work, i copied it into an empty functions.php that i placed in my child folder, also i didnt forget the <?php ?> tag around it.
Is there anything else i need to do to make it work?
Thank you Sofian
Here is the content of my functions.php:
<?php add_filter( 'wp_nav_menu_objects', 'add_menu_parent_class' ); function 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[] = 'menu-parent-item'; } } return $items; } ?>