WordPress: Display only parent category on post loop

How to display only parent category (without child categories) on a post loop?

I have:

Read More
<span class="entry-category"><?php the_category(', '); ?></span>

What should I do?

Related posts

Leave a Reply

2 comments

  1. foreach (get_the_category() as $cat) {  
        $parent = get_category($cat->category_parent);
        if (!get_category($cat->category_parent)) {
            // do something with the category, like use it to create a new query_posts or something
        }
    }
    
  2. You can get category and check parent field for each entry like;

    <?php
        $categories = get_the_category();
        if($categories){
            foreach($categories as $category) {
                if ($category->parent < 1) {
                    // Your action here
                }
            }
        }
    ?>