0

I am traversing through categories using function like this

function hierarchical_term_tree($category = 0)
{
    $r = '';

    $args = array(
        'parent' => $category,
    );

    $next = get_terms('product_cat', $args);

    if ($next) {
        $r .= '<ul>';

        foreach ($next as $cat) {
            $r .= '<li><a href="' . get_term_link($cat->slug, $cat->taxonomy) . '" title="' . sprintf(__("View all products in %s"), $cat->name) . '" ' . '>' . $cat->name . ' (' . $cat->count . ')' . '</a>';
            $r .= $cat->term_id !== 0 ? hierarchical_term_tree($cat->term_id) : null;
        }
        $r .= '</li>';

        $r .= '</ul>';
    }

    return $r;
}

Thanks to deadlyhifi

But by default $cat->count includes only direct post count, but I need to get post count including nested categories. (Sum of all nested + current)

Is there any way to get such count value using wordpress functions and queries or I should implement this by myself ?

Thanks.

3
  • @N00b by default categories widget show count including nested categories I have just looked through the implementation they are using wp_list_categories
    – CROSP
    Commented May 9, 2016 at 16:59
  • 1
    You're already getting all the terms you have with this function because it repeats itself. You need a variable for each parent that represents the total count and add to this value each time it's child gets processed.
    – N00b
    Commented May 9, 2016 at 17:16
  • @N00b yes I understand how can I achieve this, but I was wondering if maybe wordpress provides already such functionality (passing some argument or whatever), Thanks for answer.
    – CROSP
    Commented May 9, 2016 at 17:22

0

Your Answer

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