3

I'm trying to make a category counter, but it's not really working. I want to have a common counter of all categories (1168), which I would later display. For example:

  • 1 category is the first level
  • 6 categories of the second level
  • 13 categories of the third level
  • 56 categories of the fourth level
  • 158 categories of the fifth level
  • 934 categories of the sixth level

How do I implement this with get_terms?

Things I've tried so far:

I have already tried a lot of things. As a result, I began to manually select the ID of the categories I needed just to summarize ... But it is so long and expensive.

// ----- 2 level class -----
$args = array('parent' => 5593);
$categories_cnt = count(get_categories( $args ));
$type1 = $categories_cnt;
// echo 'There are '. $categories_cnt .' subcategories in this category';
$args = array('parent' => 1104);
$categories_cnt = count(get_categories( $args ));
$type2 = $categories_cnt;
// echo 'There are '. $categories_cnt .' subcategories in this category';
$args = array('parent' => 2266);
$categories_cnt = count(get_categories( $args ));

$type3 = $categories_cnt;
//echo 'There are '. $categories_cnt .' subcategories in this category';
$args = array('parent' => 3507);
$categories_cnt = count(get_categories( $args ));
$type4 = $categories_cnt;
// echo 'There are '. $categories_cnt .' subcategories in this category';
$args = array('parent' => 4102);
$categories_cnt = count(get_categories( $args ));

$type5 = $categories_cnt;
// echo 'There are '. $categories_cnt .' subcategories in this category';
$args = array('parent' => 4376);
$categories_cnt = count(get_categories( $args ));
$type6 = $categories_cnt;
// echo 'There are '. $categories_cnt .' subcategories in this category'; $summ = $type1 + $type2 + $type3 + $type4 + $type5 + $type6;
echo ($summ).' 3vl';

// ----- 3 level -----
// 5596 5599 5603 5605 5607 5610 5613 1609 1613 1615 1617 1634 1636 1638 1640 1642 1649 1660 1664 1748 1751 1762 1764 1766 1768 1772 1774 1777 1799 1801 1803 1823
// 1834 1836 1147 1208 1219 1222 1236 1238 1240 1214 1242 1244 1246 1262 1267 1271 1274 1281 1287 1160 1162 1171 1173 1175 1164 1185 1189 1191 1198 1200 1203 1205
// 1217 1298 1310 1312 1315 1326 1328 1331 1337 1340 1344 1346 1349 1352 1357 1361 1363 1365 1374 1376 1378 1381 1385 1387 1389 1391 1393 1395 1399 1401 1406
// wp_list_categories ();

And so on ... I tried to make the wp_list_categories function (there you can use the depth parameter), but this function does not know how to count how many elements are returned.

  • 1
    Hi, have you already tried anything? Could you show us your code? – Krzysiek Dróżdż Apr 19 at 10:04
  • I have already tried a lot of things. As a result, I began to manually select the ID of the categories I needed just to summarize ... But it is so long and expensive. – Василий Маркин Apr 19 at 10:08
  • //-------------------------------- 2 level class---------------------------------------- $args = array('parent' => 5593); $categories_cnt = count(get_categories( $args )); $type1 = $categories_cnt; //echo 'There are '. $categories_cnt .' subcategories in this category'; $args = array('parent' => 1104); $categories_cnt = count(get_categories( $args )); $type2 = $categories_cnt; //echo 'There are '. $categories_cnt .' subcategories in this category'; $args = array('parent' => 2266); $categories_cnt = count(get_categories( $args )); – Василий Маркин Apr 19 at 10:17
  • $type3 = $categories_cnt; //echo 'There are '. $categories_cnt .' subcategories in this category'; $args = array('parent' => 3507); $categories_cnt = count(get_categories( $args )); $type4 = $categories_cnt; //echo 'There are '. $categories_cnt .' subcategories in this category'; $args = array('parent' => 4102); $categories_cnt = count(get_categories( $args )); – Василий Маркин Apr 19 at 10:18
  • $type5 = $categories_cnt; //echo 'There are '. $categories_cnt .' subcategories in this category'; $args = array('parent' => 4376); $categories_cnt = count(get_categories( $args )); $type6 = $categories_cnt; //echo 'There are '. $categories_cnt .' subcategories in this category'; $summ = $type1 + $type2 + $type3 + $type4 + $type5 + $type6; echo ($summ).' 3vl'; – Василий Маркин Apr 19 at 10:18
2

This is a bit broad to give you the full code, so 'I'll give you an outline. The idea is to loop through all categories and count how many parents each category has using get_term_parents_list (please look carefully what this function returns). This will tell you how deep the level of this category is. Then store the result in a multidimensional array. Like this:

$cat-levels = array(); // multidimensional array in which to store results
$categories = get_categories(); // retrieve an array of all categories (as objects)
foreach ($categories as $category) {
  $parents = get_term_parents_list ($category->ID, 'category');
  $count = // write code to extract amount of parents returned
  $cat-levels[$count][] = $category;
  }

Now $cat-levels[0] will contain all categories without parents, $cat-level[1] will have the categories one level deep. And so on.

  • I came to another solution ... in the next post you can see the code. But I don’t understand yet ... How can I transfer the received "$ term" data to a variable, in order to transfer it to the "include" parameter array in the next request cycle "get_categories". But the "$ term" data is passed to like this: "stdClass Object ([parent] => 3364)" And I need to transmit just the parent's ID, separated by commas. That is, like this: "3364, 1103, 2024". This is real ? – Василий Маркин Apr 19 at 19:14
  • And the logic is ... First, I choose the ID of parents of categories that have no children (that is, this is the depth of the hierarchical levels). 2. Immediately I do a calculation of how many categories are selected according to my request (without children). 3. Then I want to add the resulting result from the array to the normal view (this is the parent ID, separated by commas). 4. Transfer the result to the variable "$ X", to then call the variable "$ X" in the array "'include' => array ($ X)" And so on ... Until I reach the top level of the parent. – Василий Маркин Apr 19 at 20:20
  • Only so far I can not, process the data "$ term" and transfer it to the array "include". – Василий Маркин Apr 19 at 20:20
  • $terms = get_categories( array( 'taxonomy' => array( 'category' ), 'hide_empty' => 0, 'fields' => 'id=>parent', 'cache_domain' => 'core', 'childless' => true ) ); echo count($terms).' qty. 6 LVL <br>' ; foreach( $terms as $term->parent){ print_r($term); unset($term); } – Василий Маркин Apr 19 at 20:26
  • If you have your own solution, please share it as an answer, not as a comment. – cjbj Apr 20 at 9:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.