Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

how do I get names of child categories of an arbitrary Post?

For example i got category slug - 'motorbikes' and I got subcategories for it. So I need to get each posts subcategory name thats inside category with slug motorbikes. Y2ok

share|improve this question
    
Can you explain the relevance of the category slug changing, as opposed to the question merely being a matter of getting the child categories of a category parent? –  Chip Bennett Oct 20 '11 at 19:08
    
Hello, normally category slug is 'category', but for me it's 'motorbikes', and inside category slug 'motorbikes' i got subcategories. In subcategories i Got posts, and I need to get subcategory name with $cat = get_the_category($post->ID); $category_name = $cat->name; , but I can't cause it returns empty array. –  Y2ok Oct 20 '11 at 19:18
    
I still don't understand. What do you mean by "normally category slug is 'category'"? Are you by chance talking about the category URL base? –  Chip Bennett Oct 20 '11 at 19:30
    
Hello, yes exactly. –  Y2ok Oct 20 '11 at 19:32
    
Umm... that's different from your comment below. So, unless my edited answer answers your question, I'm still confused about what you need. –  Chip Bennett Oct 20 '11 at 19:41
add comment

1 Answer

up vote 6 down vote accepted

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.

share|improve this answer
    
Hello, thank you, but how can I get subcategory name of $post->ID , cause $cat = get_the_category($post->ID); $category_name = $cat->name; won't work. –  Y2ok Oct 20 '11 at 19:23
    
So, are you trying to get the child categories of an arbitrary post, rather than the child categories of a known category, 'motorbikes'? –  Chip Bennett Oct 20 '11 at 19:33
    
Hello, yes, could you help me with that? –  Y2ok Oct 20 '11 at 19:36
2  
Can you please explain where on earth "category slug is 'motorbike'" is coming from? –  Chip Bennett Oct 20 '11 at 19:57
2  
+1 for the great patience –  soulseekah Oct 21 '11 at 6:28
show 5 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.