0

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.
2
  • what's your table prefix?
    – Milo
    Commented Jul 23, 2012 at 21:50
  • $table_prefix = 'wp_'; Commented Jul 23, 2012 at 21:50

1 Answer 1

2

You can't shouldn't hardcode table names into queries, especially with multisite. Swap $wpdb->postmeta for wp_postmeta and it'll likely work.

edit- technically you can, edited for clarity.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.