Get a category's subcategories

Use get_categories( $args ), where $args can be a string or an associative array like below:

$args = [
	'type'                     => 'post',
	'child_of'                 => 0,        // term or category id of parent
	'parent'                   => '',
	'orderby'                  => 'name',
	'order'                    => 'ASC',
	'hide_empty'               => 1,
	'hierarchical'             => 1,
	'exclude'                  => '',
	'include'                  => '',
	'number'                   => '',
	'taxonomy'                 => 'category',
	'pad_counts'               => false 
];

Example:

$subcats = get_categories(['child_of'=>get_category_by_slug('wordpress')->term_id, 'hide_empty'=>false]);
   
/* Returns an array (of objects) of said subcategory with the following fields:
$subcats[0]['term_id']
$subcats[0]['name']   
$subcats[0]['slug']
$subcats[0]['term_group']
$subcats[0]['term_taxonomy_id']
$subcats[0]['taxonomy']   
$subcats[0]['description']   
$subcats[0]['parent']   
$subcats[0]['count']   
$subcats[0]['object_id']   
$subcats[0]['cat_ID']   
$subcats[0]['category_count']   
$subcats[0]['category_description']
$subcats[0]['cat_name']
$subcats[0]['category_nicename']
$subcats[0]['category_parent']
*/

// A for-each loop is always used when calling this function
foreach ($subcats as $subcat) {
  echo $subcat->term_id; // or whatever field it is you're interested in   
}

Get a category's link/URL

get_category_link( $category_id )
      
//-> String URL

Get the data of any category

get_term_by( $field, $value, $taxonomy, $output, $filter )

Will return false if $taxonomy does not exist or term was not found; otherwise, it returns an object or array of fields specified in the $output parameter.

Fields returned are:

  • term_id
  • name
  • slug
  • term_group
  • term_taxonomy_id
  • taxonomy
  • description
  • parent
  • count

A notice about string vs integer confusion. Field values, including term_id are returned in string format! So before you use these field values further, you must typecast numeric values (which is of string type) to actual integers, otherwise WordPress gets confused and will not return the proper value.

Get the category ID of the current/active page

// It's a global WP variable
$cat

Get the category ID of any category on any page

get_cat_ID('Category Name')