DIsplay only child category with the_category(); - with one category too (1 post)

  1. graphers
    Posted 2 years ago #

    I have a site with a widget displaying last posts on the main page.
    The widget outputs the category of a post using the_category(); tag.
    My problem is that if the post added to multiple (or parent-child) categories, the categories are all displayed,but i want to display only child category.
    I've tried keesiemeijer's solution from this topic:

      foreach((get_the_category()) as $category) {
        if ($category->category_parent  != 0) {
        echo '<a href="' . get_category_link( $category->term_id ) . '" title="' . esc_attr(strip_tags($category->name)) . '" ' . '>' . $category->name.'</a> ';

    It works fine except one thing:if the post is added to one and only category, nothing is displayed at all.
    How can i manage to fix this?
    Thanks in advance.

Topic Closed

This topic has been closed to new replies.

About this Topic