admin管理员组

文章数量:1318570

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

abc/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?

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

abc/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?

Share Improve this question edited Oct 20, 2020 at 16:41 cjbj 15k16 gold badges42 silver badges89 bronze badges asked Oct 20, 2020 at 16:21 OmicansOmicans 1012 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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.

本文标签: categoriesGet multiple category names from permalink