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.

Iam looking for a solution that the number of child post displayed, like so:

Parent (14) - Child - Child

Parent (2) - Child

I have played with the custom walker and tried the function with ->count but without luck.

Has someone a solution? Thanks

class Count_Post_Walker extends Walker_Nav_Menu
{
    function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
        global $wp_query;
        $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';

        $class_names = '';

        $classes = empty( $item->classes ) ? array() : (array) $item->classes;
        $classes[] = 'menu-item-' . $item->ID;

        $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
        $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';

        $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
        $id = $id ? ' id="' . esc_attr( $id ) . '"' : '';

        $output .= $indent . '<li' . $id . $class_names .'>';

        $attributes  = ! empty( $item->attr_title ) ? ' title="'  . esc_attr( $item->attr_title ) .'"' : '';
        $attributes .= ! empty( $item->target )     ? ' target="' . esc_attr( $item->target     ) .'"' : '';
        $attributes .= ! empty( $item->xfn )        ? ' rel="'    . esc_attr( $item->xfn        ) .'"' : '';
        $attributes .= ! empty( $item->url )        ? ' href="'   . esc_attr( $item->url        ) .'"' : '';

        $submenus = $depth == 0 ? get_posts( array( 'post_type' => 'post', 'show_count' => 1, 'numberposts' => -1,  'meta_query' => array(
       array(
            'taxonomy' => 'category',
            'field' => $item->ID,

        ))
 ) ) : false;
        $item_output = $args->before;
        $item_output .= '<a'. $attributes .'>';
        $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
        $item_output .= '</a>';
        $item_output .= $submenus ? ' <span class="submenus-count">(' . count( $submenus ) . ')</span>' : '';
        $item_output .= $args->after;

        $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
    }
}
share|improve this question
    
Could you show us the current output? –  kaiser Feb 26 '13 at 7:59
add comment

1 Answer

Yep, I've got the solution to this problem. You can retrieve number of child items right from menu_items retrieved by wp_get_nav_menu_items. This function retrieves a bunch of post objects containing info about 'menu_item_parent'.

Inside start_el function of Walker_Nav_Menu class, you can try the match agains $item->ID. This way:

$locations = get_nav_menu_locations();
$menu = wp_get_nav_menu_object( $locations['primary'] );
$menu_items = wp_get_nav_menu_items($menu->term_id);
$count = 0;
foreach( $menu_items as $menu_item ){
    if( $menu_item->menu_item_parent == $item->ID ){
        $count++;           
    }       
}

Than you've got the count of children items in $count variable. Here's the full code:

class Count_Post_Walker extends Walker_Nav_Menu {

function start_el(&$output, $item, $depth, $args) {
    global $wp_query;
    $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';

    $class_names = $value = '';

    $classes = empty( $item->classes ) ? array() : (array) $item->classes;
    $classes[] = 'menu-item-' . $item->ID;

    $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
    $class_names = ' class="' . esc_attr( $class_names ) . '"';

    $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
    $id = strlen( $id ) ? ' id="' . esc_attr( $id ) . '"' : '';

    $output .= $indent . '<li' . $id . $value . $class_names .'>';

    $attributes  = ! empty( $item->attr_title ) ? ' title="'  . esc_attr( $item->attr_title ) .'"' : '';
    $attributes .= ! empty( $item->target )     ? ' target="' . esc_attr( $item->target     ) .'"' : '';
    $attributes .= ! empty( $item->xfn )        ? ' rel="'    . esc_attr( $item->xfn        ) .'"' : '';
    $attributes .= ! empty( $item->url )        ? ' href="'   . esc_attr( $item->url        ) .'"' : '';

    /* Code to get children count */
    $locations = get_nav_menu_locations();
    $menu = wp_get_nav_menu_object( $locations['primary'] );
    $menu_items = wp_get_nav_menu_items($menu->term_id);
    $count = 0;
    foreach( $menu_items as $menu_item ){
        if( $menu_item->menu_item_parent == $item->ID ){
            $count++;           
        }       
    }
    $item_output = $args->before;
    $item_output .= '<a'. $attributes .'>';
    $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
    $item_output .= '</a>';
    if( $count > 0 )
        $item_output .= '('.$count.')';
    $item_output .= $args->after;

    $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
}

}

share|improve this answer
    
Sry, but i mean not to display the count of the child Cateogrys, but of the POSTS inside the Children Categorys. But i try now to find a soloution :) –  jamal Feb 26 '13 at 17:09
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.