2

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

5
  • 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? Commented Oct 20, 2011 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
    Commented Oct 20, 2011 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? Commented Oct 20, 2011 at 19:30
  • Hello, yes exactly.
    – Y2ok
    Commented Oct 20, 2011 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. Commented Oct 20, 2011 at 19:41

2 Answers 2

12

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.

8
  • 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
    Commented Oct 20, 2011 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'? Commented Oct 20, 2011 at 19:33
  • Hello, yes, could you help me with that?
    – Y2ok
    Commented Oct 20, 2011 at 19:36
  • See edited answer. Commented Oct 20, 2011 at 19:40
  • 2
    Can you please explain where on earth "category slug is 'motorbike'" is coming from? Commented Oct 20, 2011 at 19:57
0
$categories = wp_get_post_categories( get_the_ID(), array('fields' => 'ids') );

$arguments = array(
    'taxonomy'    => 'category',
    'childless'   => true,
    'include'     => $categories,
);

$selected_category = get_terms( $arguments );
1
  • 2
    Please edit your answer, and add an explanation: why could that solve the problem? See How to Answer
    – fuxia
    Commented Jul 28, 2016 at 18:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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