How to list specific subcategories for each page?

Asked

Viewed 57 times

0

I would like to list within the category page.php the sub-categories of the specific page itself, I am using wp_list_categories() and trying to pass the id with get_the_category(), but I’m not getting 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,
        ));

?>

1 answer

1

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

$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);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.