1

Let's say, I have two categories, 'color' and 'temperature'. Each one has a number of sub-categories.

Wordpress does great job displaying posts in requested categories when I go to URL like this one:

http://www.example.com/category/pink+warm

How can I get these categories (IDs or slugs) in category template?

UPDATE: Thanks to Rarst; I will analyze $wp_query->tax_query->queries.

ps. btw, http://www.example.com/category/blue,yellow gives posts in any of blue OR yellow categories. Nice ,)

pps. AND there may be more than two categories, of course.

0

Hm hm. WordPress might be good at making this work, but isn't too good at making it convenient to work with. Typically get_queried_object() is a good way to access such context, but in this case it will merely hold the first term and ignore the rest.

You will probably have to scrape this information out of $wp_query->tax_query->queries, which will hold something like this:

array(2) [
    array(5) [
        'taxonomy' => string (8) "category"
        'terms' => array(1) [
            string (12) "post-formats"
        ]
        'field' => string (4) "slug"
        'operator' => string (2) "IN"
        'include_children' => bool TRUE
    ]
    array(5) [
        'taxonomy' => string (8) "category"
        'terms' => array(1) [
            string (6) "markup"
        ]
        'field' => string (4) "slug"
        'operator' => string (2) "IN"
        'include_children' => bool TRUE
    ]
]
  • Yes, I've already played with this a bit; I wish there would be an API call .) I'll accept your answer if there is no better way; I'll wait a bit. Thank you! – brownian Dec 27 '14 at 17:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.