According to a number of references online, I should be able to use the following function to add the class, has-children
, to menu items that have children under them:
function gtp_nav_menu_css_class( $css_class, $item ) {
global $wpdb;
$has_children = $wpdb->get_var("SELECT COUNT(meta_id) FROM wp_postmeta WHERE meta_key='_menu_item_menu_item_parent' AND meta_value='" . $item->ID . "'");
if ($has_children > 0) {
array_push($css_class, 'has-children');
}
return $css_class;
}
add_filter( 'nav_menu_css_class', 'gtp_nav_menu_css_class', 10, 4 );
I am using wp_nav_menu()
in my header.php
. However, after adding this code to functions.php
, I am not seeing the has-children
class being added to any menu items (when in fact there are menu items that contain children). Does anybody know what may be the culprit?
Notes:
- I am using WP 3.4.1.
- I am running a Multisite setup
- Table prefix is 'wp_'
- Via debug echo statements, I can see that the function is getting executed, but not inside the conditional statement.
$table_prefix = 'wp_';