Forums

[resolved] How to print only sub-category name (2 posts)

  1. ucarmetin
    Member
    Posted 11 months ago #

    Hey!

    I'm trying to figure out how to print only sub-category of a given category. There is one parent category, called "Local". This parent category has six sub-categories, each of them is a city name. Each post is associated with the parent category, Local, and one of the sub-categories.
    For example,

    Post A - Local and City 1
    Post B - Local and City 2

    I want to print out only the sub-category name within the loop. I've come across the following code in wordpress forums but it doesn't help me as it prints out the parent category name as well.

    <?php foreach((get_the_category()) as $category) { echo ( $category->cat_name != 'local' ) ? $category->cat_name . ' ' : ''; } ?>

    Any help will be greatly appreciated.
    Thanks.

  2. ucarmetin
    Member
    Posted 11 months ago #

    Hey!

    I solved the problem. It was due to the fact that I used small "l" instead of capital "L" when writing the code. In case someone else wonders how to print out only sub category name, here is the code.

    <?php foreach((get_the_category()) as $category) { echo ( $category->cat_name != 'Local' ) ? $category->cat_name . ' ' : ''; } ?>

Reply

You must log in to post.

About this Topic