4

I have a custom walker menu.( See the code below)

I would like to have my nav menu like this:

enter image description here

Where 495 is the total of the records containing childrens.

Where 42 Number of published articles.

Can someone help me to implement this? Here is my current custom nav walker code.

<?php
class accordion_Menu_Walker extends Walker_Nav_Menu {
   function start_el(&$output, $item, $depth, $args) {
        global $wp_query;  
        $count_category = '';
     if($item->object == 'publishings')  { 
        $cat_id = $item->object_id;
        $count_category = get_terms('publishings', 'fields=count');
   }           
        $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        ) .'"' : '';

        $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 .= '(' . $count_category  . ')';
        $item_output .= '</a>';
        $item_output .= $args->after;

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

The code that I use for taxonomy:

add_action( 'init', 'prowp_publishing_taxonomy' );
function prowp_publishing_taxonomy() {
    $labels = array(
        'name'              => 'Категории',
        'singular_name'     => 'Категория',
        'search_items'      => 'Найти Категорию',
        'all_items'         => 'Все категории',
        'parent_item'       => 'Родительская категория',
        'parent_item_colon' => 'Родительские категории:',
        'edit_item'         => 'Редактировать категорию',
        'update_item'       => 'обновить категорию',
        'add_new_item'      => 'Добавить новую категорию',
        'new_item_name'     => 'Новое название категории',
        'menu_name'         => 'Категории'
    );
    $args =array(
        'labels'       => $labels,
        'hierarchical' => true,
        'query_var'    => true,
        'rewrite'      => true
    );
    register_taxonomy( 'type', 'publishings', $args );
}

Help me please do it.

display my current result:

enter image description here

2
  • Can you display your current result?
    – m4n0
    Commented May 1, 2015 at 8:23
  • I'm creating a website on a local server. I can not show it. The result is on the picture below. Digits are not visible but brackets ( ) are visible.
    – Chipsed
    Commented May 1, 2015 at 10:39

1 Answer 1

1

I think you should probably use wp_list_categories(), which even has parameters for a count, like e.g. show_count and pad_counts. Additionally it supports custom walkers via the walker parameter, but the walker would be based on Walker_Category - source -, which gives you every additional styling option you want.

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.