R
R
Roman2016-02-04 16:39:00
PHP
Roman, 2016-02-04 16:39:00

Wordpress Walker - how to add number of entries to menu items (categories)?

The main menu consists of links to category pages.
I want to display the number of entries in it inside each link after the name of the category.
Editing walker .
I get quantity:

$item_count = $depth == 0 ? get_posts( array(
  'post_type' => 'post',
  'numberposts' => -1,
  'orderby' => 'category',
  'order' => 'ASC'
) ) : false;

Menu output:
$item_output .= $item_count ? ' <span class="item_count">(' . count( $item_count ) . ')</span>' : '';

As a result, I get an addition to each menu item of the number of all records.
How to improve?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman, 2016-02-04
@llgruff

We get it via get_category:

if ($item->type == 'taxonomy') {
  $cat = get_category( $item->object_id);
  $item_output .= ' ('.$cat->count.')</a>';
}

Applicable to Bootstrap Dropdown Menu Using a Custom Nav Walker

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question