To get the child categories of a given parent category slug, use a combination of get_category_by_slug()
and get_categories()
.
The latter function will return an array of category objects matching the specified query arguments array; the former will return the ID of a category, given its slug.
So, for example:
<?php
$motorbike_child_cat_args = array(
'child_of' => get_category_by_slug( 'motorbikes' )
);
$motorbike_child_cats = get_categories( $motorbike_child_cat_args );
?>
Then, you can do whatever you want with your array-of-category-objects. For example, to get an array of child-category names:
<?php
$motorbike_child_cat_names = array();
foreach ( $motorbike_child_cats as $child_cat ) {
$motorbike_child_cat_names[] = $child_cat->name;
}
?>
Really, what you do with it is up to you at that point.
EDIT
If you need to get child categories of an arbitrary post, then you can use get_the_category()
.
If you’re inside the Loop, simply call get_the_category()
; if you’re outside the Loop, then you need to pass the Post ID to the call: get_the_category( $id )
.
So, for example, to build an array of names of child categories (regardless of parent) of the current post:
<?php
$my_post_categories = get_the_category();
$my_post_child_cats = array();
foreach ( $my_post_categories as $post_cat ) {
if ( 0 != $post_cat->category_parent ) {
$my_post_child_cats[] = $post_cat->cat_name;
}
}
?>
Or, for example, to build an array of names of ‘motorbike’ child categories of the current post:
<?php
$my_post_categories = get_the_category();
$motorbikes_child_cats = array();
foreach ( $my_post_categories as $post_cat ) {
if ( 'motorbikes' == $post_cat->category_parent ) {
$motorbikes_child_cats[] = $post_cat->cat_name;
}
}
?>
Is that more what you’re looking for?
EDIT 2
If you just need to get all the categories of your post:
<?php
$all_post_categories = get_the_category();
$my_post_cats = array();
foreach ( $my_post_categories as $post_cat ) {
$my_post_cats[] = $post_cat->cat_name;
}
?>
That will give you all of the categories for the current Post. I have no idea how motorbikes
category slug factors into this question.