Wordpress: Retrieve post categories outside of the loop


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?php
  2. $ID = $wp_query->posts[0]->ID;
  3. $postcat = get_the_category($ID);
  4. $cat = $postcat[0]->cat_ID;
  5. $parent = get_category ($cat);
  6. if ($parent->parent) {
  7. wp_list_categories ('child_of=' . $parent->parent);
  8. } else {
  9. wp_list_categories ('child_of=' . $cat);
  10. }
  11. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.