0

I am trying to get category names for the ones that are available in permalink. e.g. I have the following permalink structure.

abc.com/product-category/category-1/category-2/

Where as category-1 and category-2 both are categories. When I try $wp_query->get_queried_object(), it just returns info related to category-2. How can I get details of category-1 using any existing WordPress functions?

1 Answer 1

0

The function get_category_parents will get you a string of the parent categories given the ID of a child category. So, like this:

$cat = $wp_query->get_queried_object();
$catid = $cat->cat_ID;
$parentcats = get_category_parents ($catid, false, '|', false);

This will give you something like category1|category2|category3. Follow the link above to see what other options you have. If you want to do more complicated stuff, you could split the string using explode.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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