I'm trying to collect a list of all categories used on posts. However, the following code only returns the first category. How could this be adapted to collect all categories, if the post has multiples?

$category = get_the_category();
$the_cats[] = $category[0]->term_id;

Ultimately, I would like to use the term_id list to show/hide HTML elements on the page, depending on the selection.


Try this function


Check more reference from here: https://developer.wordpress.org/reference/functions/wp_get_post_categories/

  • Awesome. I've now got a working array. Thanks for the pointer... Jun 16 '16 at 11:48

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.