WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. graphers
    Member
    Posted 11 months ago #

    Hi,
    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:

    <?php
      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.

Reply

You must log in to post.

About this Topic