Damian
Damian

Reputation: 608

Count the number of subcategories in wordpress

I need to count number of child categories in Wordpress but i can not deal with it. Somebody know how to do this?

Upvotes: 0

Views: 2447

Answers (3)

YKKY
YKKY

Reputation: 605

$args = array('parent' => YOUR_PARENT_CATEGORY_ID_NUMBER);
$categories_cnt = count(get_categories( $args ));

echo 'There are '. $categories_cnt .' subcategories in this category';

Is that what you were looking for?

Upvotes: 0

Pankaj Sharma
Pankaj Sharma

Reputation: 679

You can count all the categories and subtract parent categories from them to get the child categories.

$total_categories_count  = wp_count_terms('category');
$parent_categories_count = count(get_categories('parent=0&hide_empty=0'));
$child_categories_count  = $total_categories_count - $parent_categories_count;

Hope this helps.

Upvotes: 3

ivanfromer
ivanfromer

Reputation: 329

mysql directly:

you should try something like:

SELECT categoria.id AS id_categoria, categoria.nome AS nome_categoria,
        subcategoria.id AS id_subcategoria, subcategoria.nome AS nome_subcategoria
            FROM categoria
            INNER JOIN subcategoria
            ON categoria.id = subcategoria.id_categoria
            ORDER BY nome_categoria, nome_subcategoria

Upvotes: 0

Related Questions