L.rp
L.rp

Reputation: 943

List all taxonomies/categories and their children in Wordpress (even if they are empty)

I want to display all child categories in a post. Currently I can list the post categories, but not all child categories that are in the category.

I tried:

print_r (get_categories(array('child_of' => 2)));

but that function displays only the categories for the post and not all child categories.

Is there a function in Wordpress function to get all children as well as the categories?

Upvotes: 0

Views: 3374

Answers (1)

FluffyKitten
FluffyKitten

Reputation: 14312

get_categories should return child categories by default so if you are not getting any I suspect its because they are empty (by default get_categories only returns categories that have posts associated with them).

There are a few ways of getting all categories, depending on what you need it for:

1. Get a flat list of categories and children

You can use get_categories to get all return all categories and subcategories in an array even if they have no posts by setting "hide_empty" to 0:

$cat_id = 2; // category id to get (for ALL categories, change to 0 or remove from arg array)
$categories = get_categories(array("child_of" => $cat_id, "hide_empty" => 0));

// print all category names
foreach ($categories as $category)
    echo $category->name;

Return value: returns an array with all categories and child categories. Note that this is does not have a hirearchical structure, i.e. parents and children are all at the same level

2. Display a hierarchical list with links to the archive pages

wp_list_categories will return all categories and children as a list, and include links to the archive pages.

$cat_id = 2; // category id to get (for ALL categories, change to 0 or remove from arg array)
$categories = wp_list_categories(array("child_of" => $cat_id, "echo" => 1, "hide_empty" => 0));
echo $categories;

If hide_empty is 1 it will only return categories that have posts, so set it to 0 to return all categories.

There are many other parameters, including "depth" to specify how many levels to return, and "orderby" to specify the order. See Developer Reference

Return value: the html for a hierarchical ul unordered list of all categories, with child categories nested under their parent. All categories are displayed by name with a link to the category archive page.

3. Get a hierarchical array of category objects

I've written the following function that uses recursion to get a multi-dimensional array of all categories/taxonomy terms, where the children are nested under the parent.

Parameters:

  • $catId If you want to just get the children for a specific category, pass the category ID. For all categories, use 0
  • $taxonomy Pass the taxonomy name as the second parameter. Defaults to "category"
  • $bEchoList If you want to print the category/term names as a hierarchical unordered list, pass true as the last parameter.

The function:

function getCategoryHierarchy($catId=0, $taxonomy="category", $bEchoList = false ){

    $args = array(
        "hide_empty" => 0,
        "hierarchical" => 1,
        "taxonomy"=> $taxonomy,
        "parent" => $catId
    );

    $categories = get_categories($args);

    if(count($categories) > 0){

        if ($bEchoList) echo "<ul>";

        foreach ($categories as $category) {
            if ($bEchoList)
                echo "<li>".$category->cat_name."</li>";

            $cats[$category->cat_ID]["category"] =  $category;

            $children = getCategoryHierarchy($category->cat_ID, $taxonomy, $bEchoList );
            if ($children)
                $cats[$category->cat_ID]["children"] = $children;
        }
        if ($bEchoList) echo "</ul>";
    }
    return $cats;
}

$cat_id = 2; // category id to get (for ALL categories, change to 0 or remove from parameters)
$categories = getCategoryHierarchy($cat_id, "category", true );


Return value: returns a hierarchical array with all categories/taxomony terms and children. These are nested hierarchically, with the category id as the array.

Each category array has 2 "sub-arrays"

  • "category" key: contains the the category WP_Term object
  • "children" key: if the category has any children, they will be included here in the same structure (i.e. with category and children sub-arrays)

Return array structure:

[*CATID*] => Array(
        [category] => WP_Term Object(...)
        [children] => Array(

                [*CATID*] => Array(
                        [category] => WP_Term Object(...)
                        [children] => Array(

                               [*CATID*] => Array(
                               [category] => WP_Term Object(...)
                           )
                    )

               [more children...]
            )
    )

[*CATID*] => Array(
        [category] => WP_Term Object(...)
    )

    [more categories...]

Upvotes: 4

Related Questions