Search code examples
wordpresscategorieswp-list-categories

Displaying the name of a child category without listing all childs


I'm trying to display the name of a child category attached to a post using this function:

<?php wp_list_categories('style=none&orderby=id&show_count=0&use_desc_for_title=0&child_of=39&hide_empty=1'); ?>

The problem is that it lists all the child category names of parent 39. I would only like to get the category name that is used by the post.

Thanks in advance


Solution

  • Thanks to @Siyah, I found the answer here

    <?php
    foreach((get_the_category()) as $childcat) {
    if (cat_is_ancestor_of(7, $childcat)) {
    echo $childcat->cat_name;
    }}
    ?>