Display child category in single post (WordPress)

i am trying to create a blog and a portfolio (both in one) wordpress theme

im using categories for my posts (as many other blogs). i am using one category named portfolio, so that i can distinguish all my portfolio post from the blog posts.

Read More

when use click on one of the portfolio post they go to a page, where they get some pictures of the project and explenation. they also will see the type of project it is:
web design,
graphic design,
print,
demos

all four above are child categories of the portfolio category. therefore when users click on the project post, they need to see the type of project it is

so is there a way to show the child category of the post
i tried the_category() – it shows the parent and the child – I tried the_category(‘exclude=10’) nothing comes up (not even an error message)

i would be grateful if you could give me an idea of how to show the child category only.

also, is it ok (good practice) the fact that i decided to use child categories for portfolio posts

thanks a lot

Related posts

Leave a Reply

1 comment

  1. i found the solution to my problem

    foreach ((get_the_category()) as $childcat) {
        if (cat_is_ancestor_of(10, $childcat)) {
            echo '<a href="'.get_category_link($childcat->cat_ID).'">';
            echo $childcat->cat_name . '</a>';
        }
    }
    

    this is what the author says:

    It takes the categories of the post
    being displayed, checks to see if one
    is an ancestor of the chosen parent
    (’10’ in my case), and outputs the name
    of that child category in the echo.

    I found it here

    it works as i needed it to so im happy