How to show categories of posts in a WordPress theme?

To get the categories from a WordPress site we use the get_Categories function. If you look up the tables in a WordPress database you will not find any table called categories. Data is stored in 
Category data is stored in the wp_terms table

and wp_term_taxonomy.

The get_Categories function returns an array of category objects. Have a look at the possible parameters of the get_Categories($terms) function.

The possible values for the $terms function are:

array (
'taxonomy' => 'category', //empty string(''), false, 0 don't work, and return empty array
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => true, //can be 1, '1' too
'include' => 'all', //empty string(''), false, 0 don't work, and return empty array
'exclude' => 'all', //empty string(''), false, 0 don't work, and return empty array
'exclude_tree' => 'all', //empty string(''), false, 0 don't work, and return empty array
'number' => false, //can be 0, '0', '' too
'offset' => '',
'fields' => 'all',
'name' => '',
'slug' => '',
'hierarchical' => true, //can be 1, '1' too
'search' => '',
'name__like' => '',
'description__like' => '',
'pad_counts' => false, //can be 0, '0', '' too
'get' => '',
'child_of' => false, //can be 0, '0', '' too
'childless' => false,
'cache_domain' => 'core',
'update_term_meta_cache' => true, //can be 1, '1' too
'meta_query' => '',
'meta_key' => array(),
'meta_value'=> '',
);

Let us experiment with some queries based on the following categories that I just entered in the WordPress site.

The present list of categories in the site is given below.

Let us try calling the get_Categories() functions and print the output using print_r().

Here is the code:

<?php
$args=array(
'hide_empty' => false
);
$categories=get_Categories($args);
print_r($categories);
?>

The hide_empty option causes categories with zero posts to be displayed.

Array ( [0] => WP_Term Object ( [term_id] => 19 [name] => Arrays in C [slug] => arrays-in-c [term_group] => 0 [term_taxonomy_id] => 19 [taxonomy] => category [description] => Arrays in C [parent] => 17 [count] => 0 [filter] => raw [cat_ID] => 19 [category_count] => 0 [category_description] => Arrays in C [cat_name] => Arrays in C [category_nicename] => arrays-in-c [category_parent] => 17 ) [1] => WP_Term Object ( [term_id] => 18 [name] => Arrays in Java [slug] => arrays-in-java [term_group] => 0 [term_taxonomy_id] => 18 [taxonomy] => category [description] => Arrays in Java [parent] => 16 [count] => 0 [filter] => raw [cat_ID] => 18 [category_count] => 0 [category_description] => Arrays in Java [cat_name] => Arrays in Java [category_nicename] => arrays-in-java [category_parent] => 16 ) [2] => WP_Term Object ( [term_id] => 17 [name] => C [slug] => c [term_group] => 0 [term_taxonomy_id] => 17 [taxonomy] => category [description] => C programming [parent] => 15 [count] => 0 [filter] => raw [cat_ID] => 17 [category_count] => 0 [category_description] => C programming [cat_name] => C [category_nicename] => c [category_parent] => 15 ) [3] => WP_Term Object ( [term_id] => 16 [name] => Java [slug] => java [term_group] => 0 [term_taxonomy_id] => 16 [taxonomy] => category [description] => java programming [parent] => 15 [count] => 0 [filter] => raw [cat_ID] => 16 [category_count] => 0 [category_description] => java programming [cat_name] => Java [category_nicename] => java [category_parent] => 15 ) [4] => WP_Term Object ( [term_id] => 15 [name] => Programming [slug] => programming [term_group] => 0 [term_taxonomy_id] => 15 [taxonomy] => category [description] => Category for programming language. [parent] => 0 [count] => 0 [filter] => raw [cat_ID] => 15 [category_count] => 0 [category_description] => Category for programming language. [cat_name] => Programming [category_nicename] => programming [category_parent] => 0 ) [5] => WP_Term Object ( [term_id] => 14 [name] => Uncategorized [slug] => uncategorized [term_group] => 0 [term_taxonomy_id] => 14 [taxonomy] => category [description] => Uncategorized [parent] => 0 [count] => 0 [filter] => raw [cat_ID] => 14 [category_count] => 0 [category_description] => Uncategorized [cat_name] => Uncategorized [category_nicename] => uncategorized [category_parent] => 0 ) )

We have got an array of objects of type WP_Term Object. It also shows the fields inside.
Let us try and print the various fields now.

<?php
$args=array(
'hide_empty' => false,
'orderby' => 'name'
);
$categories=get_Categories($args);
echo "<br/>";
$n=count($categories);
echo "No of categories is " . $n;
echo "<br/>";
for($i=0;$i<=$n-1;$i++)
{
$category=$categories[$i];
echo "<br/><br/>";
echo "Position " . $i;
echo "<br/>";
echo "Id " . $category->term_id;
echo "<br/>";
echo "Name " . $category->name;
echo "<br/>";
echo "Slug " . $category->slug;
echo "<br/>";
echo "Count " . $category->category_count;
echo "<br/>";
echo "Parent " . $category->category_parent;
echo "<br/>";
echo "Description " . $category->description;
echo "<br/>";
}
?>

Leave a Reply