How to list specific subcategories for each page?

0

I would like to list the subcategories of the page itself, I am using wp_list_categories() and trying to pass the id with get_the_category() , but I am not having any results.

<?php

$categories = get_the_category();
    $id = $categories[0]->term_id;
    wp_list_categories( array(
        'child_of'        =>  $id,
        'show_count'      => 0,
        'orderby'         => 'name',
        'title_li'        => '',
        'order'           => 'ASC',
        'container_class' => 'menu-footer-titulo',
        'hide_empty'      => 0,
        ));

?>
    
asked by anonymous 06.04.2018 / 04:08

1 answer

1

The function get_the_category() returns an array with objects in the current category. Then I swept all elements with foreach by storing the ids of the terms in the array $cat_ids . Then I used the implode() function to transform $cat_ids to a $inc string.

$categorias = get_the_category();
foreach ($categorias as $value) {
    $cat_ids[] = $value->cat_ID ;
}
$inc = implode(',' , $cat_ids);
$args = array(  
    'include' => $inc,
    'hierarchical' => 1
);

wp_list_categories($args);
    
06.04.2018 / 06:20