Support » Fixing WordPress » Get active category of post in multiple categories

  • Some of my posts are added to multiple categories. For example, I have a parent category “Colours”, with sub-categories “Red”, “Blue” and “Green”. One of my posts, “Purple”, is added to both “Red” and “Blue” categories.

    So the hierarchy would be as follows:

    – Colours
    —– Red
    ———– Purple
    —– Blue
    ———– Purple
    —– Green

    The “Purple” post is therefore accessible via two permalinks, ie:

    That all works fine. The trouble I’m experiencing is getting the active sub-category ID when viewing the Purple page (single.php template) from within either of its multiple categories.

    I need to know, when viewing, that the sub-category is “Red”. get_the_category() is useless in this instance because it returns category ID’s for both “Red” and “Blue”.

    To be more specific, I’m trying to build a dynamic breadcrumb (mirrored as a page title) that contains the full hierarchy (as illustrated by the permalink).

    AFAIK, this question has come up a number of times but remains unanswered. Any help would be appreciated, thanks.

Viewing 14 replies - 1 through 14 (of 14 total)
  • Michael


    Forum Moderator

    do you mean you are looking for the parent category of the ‘purple’ post?
    have you tried:

    @alchymyth: no not the parent, that would be “Colours”. I’m looking for the subcategory, of which there are multiples (“Red” and “Blue”), based on which is currently active. – I need to know it’s in the “Red” subcategory – I need to know it’s in the “Blue” subcategory

    Not sure if it helps to know, but the “Purple” post is only added to the multiple subcategories, and not the parent category itself (“Colours”).

    Unfortunately this looks as if its going to stay unsolved!? Can’t believe this is not possible in WordPress, I’m looking for a similar solution for my problem, but can’t get it to work either!

    Anyway would be great to hear if the problem get’s solved some way.. cheers



    Forum Moderator

    it depends from where you look at the ‘problem’.
    and if you are interested in some help, could you please state your problem here or link to an existing thread.

    if you look from the child perspective – there is one unique parent, and grandparent, and so on. you can find them with get_category_parents.
    in the example: the position you are in is ‘purple’.

    in @codacoza‘s problem, little ‘purple’ will exactly know who is the parent, and therefore have the access to the exact permalink.

    if you are pretending that you are ‘blue’ or ‘red’, or you are looking down from ‘colours’, then you don’t know who ‘purple’ is – you get lost 😉

    @mauricenaef: for now I’m using a solution that checks the permalink (via $_SERVER[‘REQUEST_URI’]) to get the child/sub-category slug. Far from elegant, but it works! I’d prefer a more future-proof solution though.

    @alchymyth: Like I said, when working with a post in multiple categories, get_category_parents() will return all of child/sub-categories for a post (in my example, that would be an array of 2), and not the currently active category, so it’s pretty much useless. Also, a post in multiple categories has no parent.

    I’m currently experiencing a similar problem.

    Have a site where we are using the categories to create sections of the site, with a different layout for each section ( colours et al ).

    Problem is a post can be in more than one category and its then impossible to detect the ‘active category’ to show the right layout.

    Example: 2 Categories X and Y. Post Z belongs to both categories.
    I view the category archive for Y, click on post Z, single.php needs to realise its come from the Y archive and show the template for Y.

    Currently the Single.php ( obviously by design ), detects it belongs to X and Y and thus just returns X when I use the $category[0]-> method.

    The more I think about it the more I think that it can’t be done and we’ll have to stick with the $_SERVER[REQUEST_URL] method with the permalinks for detecting what “Category the CURRENT post is in”.

    I am new to wordpres, so I hope my question is addressing the same issue.

    My site if functioning perfectly, EXCEPT that it is redirecting to another category when ever you click “continue reading” at the bottom of my category post. It keeps taking me back to my product category, instead of staying on the same category that the catergory post is originally on. I want it to stay the reader to be able to view the category they were on while responding, not redirect them to the category with the widgets to my product category. I’t not bad, but I don’t want to overwhelm the potential customer, with every time he tries to blog it takes him to my product category.

    Can you help? I have researched for weeks and can not find the answer.

    My website address is
    My e-mail address is

    I would really appreciate any help that you could give me.

    Thank you.

    Omission, mauricenaef, et al,

    I agree. The lack of the ability to easily detect the current parent category for the single post you’re viewing is troubling. I can think of many reasons you’d want to do this – Breadcrumb navigation, Showing a parent category as a primary title to what you’re viewing, etc.

    Offhand, and I’m not an experienced php programmer, I think that if the next_post_link keeps you in the current category you’re viewing, then it’s detecting which category you’re in, so the answer maybe could be found by determining how that function detects that.

    For the record, I think there should be a function to detect the current category for a single post. Something akin to a get_the_category, but more powerful than the $category[0]-> method.



    The sCategory permalink plugin provides part of the solution, in that you can choose to make one category show up in the permalink by making it ‘primary’.

    I have permalinks set up.
    Before permalinks setup I have query string like this,


    And by changing it to permalinks to numeric,

    It shows ;

    There is
    po-campo1 is category
    product-3 is product in po-campo1 category.

    So, how can I get category_id and product_id at the top of the page?

    If you know that please share it with me, I really need it.

    OK people, I know this will help many:
    Highlight WordPress Category when on a Single Post: Tutorial



    Hi @codacoza,
    I’ve the same problem in my site but I can’t find any solution, can you share the solution that checks the permalink please?
    I appreciate a lot,
    Thanks in advance

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘Get active category of post in multiple categories’ is closed to new replies.