I've a list of url's and I want to get the categories of each one. Whats the best way to achieve this?

Thanks. Diego


1 Answer 1

$category_slug_array = array('slug_1', 'slug_2', 'slug_2'); //array of category slugs

foreach($category_slug_array as $category_slug){
   $my_categories[] = get_term_by( 'slug', $category_slug, 'category'); //getting category object by slug

print_r($my_categories); //printing array of categories 
  • Could you please edit your answer and add in an explanation on what this piece of code does, where to add it, and how it solves the original issue?
    – Howdy_McGee
    Commented Feb 1, 2018 at 15:35

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.