How to get the depth of a category’s subcategories

I would do it this way:

  • get all sub-categories (include empty, set up hierarchically) as array
  • retrieve the depth of the array in PHP (this part is taken (but adapted) from here)

Altogether, it’s the following code, which is bundled into a function, but could, of course, be used directly somewhere in a template file:

function get_category_depth($id = 0) {
    $args = array(
        'child_of' => $id,
        'hide_empty' => 0,
        'hierarchical' => 1,
    );
    $categories = get_categories($args)

    if (empty($categories))
        return 0;

    $depth = 1;
    $lines = explode("\n", print_r($categories, true));

    foreach ($lines as $line)
        $depth = max(
            $depth,
            (strlen($line) - strlen(ltrim($line))) / 4
        );

    return ceil(($depth - 1) / 2) + 1;
} // function get_category_depth

Please note that I did not test this.

deneme bonusu veren sitelerbahis casinomakrobetceltabetpinbahispolobetpolobet girişpinbahis girişmakrobet girişpulibet girişmobilbahis girişkolaybet giriş