21

I'm trying to get all the children categories to display in this loop but I'm struggling with the code. This is what I have so far.

<?php $args=array('orderby' => 'name', 'order' => 'ASC');
    $categories=get_categories($args); 
    foreach ($categories as $cat) { ?>
    <dt><a href="#" class="customer-acquisitiontop" id="<?php echo $cat->slug; ?>" data-filter=".<?php echo $cat->slug; ?>"><h2><?= $cat->cat_name; ?></h2></a></dt>
    <dd><div class="services">
    <?= $categories=get_categories('parent'); ?> /*This should be the children of the parent category */
    </div>
    </dd>
<?php } ?>

Any help would be great

4 Answers 4

24

You can't just pass the string "parent" to get_categories. You have to pass the ID of the parent.

$categories=get_categories(
    array( 'parent' => $cat->cat_ID )
);

Notice that there are two similar but not equal "get child" parameters that you can use.

child_of (integer) Display all categories that are descendants (i.e. children & grandchildren) of the category identified by its ID. There is no default for this parameter. If the parameter is used, the hide_empty parameter is set to false.

parent (integer) Display only categories that are direct descendants (i.e. children only) of the category identified by its ID. This does NOT work like the 'child_of' parameter. There is no default for this parameter. [In 2.8.4]

Now you need to loop over the $categories. You can't just echo an array.

foreach ($categories as $c) {
    var_dump($c);
    // what you really want instead of var_dump is something to
    // to create markup-- list items maybe, For example...
    echo '<li>'.$c->cat_name.'</li>';
}
4
  • Unfortunately, that is just giving me an output of Array. No values are being pulled in. Nov 29, 2012 at 16:37
  • 'Array' is what happens when you try to echo an array. You need to loop over the array and echo the individual elements.
    – s_ha_dum
    Nov 29, 2012 at 16:39
  • 2
    You might want to add 'hide_empty' => false. To also show empty categories.
    – Floris
    Jun 18, 2018 at 14:09
  • optionally, you can pull the category ID like this ->$categories = get_categories( array( 'parent' => get_cat_ID( 'Your Category Name' ) ) ); ... as long as the category name doesn't change
    – aequalsb
    Feb 16 at 13:28
7

Use the code below in your archive.php file. This code will help you:

<?php

    $term = get_queried_object();

    $children = get_terms( $term->taxonomy, array(
        'parent'    => $term->term_id,
        'hide_empty' => false
    ) );

    if ( $children ) { 
        foreach( $children as $subcat )
        {
            echo '<li><a href="' . esc_url(get_term_link($subcat, $subcat->taxonomy)) . '">' . $subcat->name . '</a></li>';
        }
    }
?>
1
  • Please edit your answer, and add an explanation: why could that solve the problem?
    – fuxia
    Apr 25, 2018 at 7:57
2

If there are no values in the array you can try the following approach:

$last_categories = get_categories(
  array(
    'taxonomy' => 'product_cat',
    'parent' => $sub_category->cat_ID
  )
);
-1

To get child categories you can use following code.

$category = get_queried_object(); // this is for getting the parent category on archive or any place the category object is called.
$categories=get_categories(
                        array( 'parent' => $category->term_id,
                                'hide_empty' => false )
                                ); 

Notice :- I have used 'hide_empty' => false to show categories with no any posts under it. Then use the $categories array to loop and make your markup.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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