Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.
share|improve this question
    
what's your table prefix? –  Milo Jul 23 '12 at 21:50
    
$table_prefix = 'wp_'; –  Stephen Watkins Jul 23 '12 at 21:50
add comment

1 Answer

up vote 2 down vote accepted

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.

share|improve this answer
    
Wow, fantastic. –  Stephen Watkins Jul 23 '12 at 22:04
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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