get_the_category returns category for a page
-
I was testing the search.php functionality on a theme I am hacking.
In The Loop it calls the_category() to list the categories that a post is published under e.g. “Published in Cat1, Cat2 etc”As pages do not have categories I am calling going through get_the_category() to count the categories. If not zero then I display “Published in” and call the_category().
For one page get_the_category() says it is in the “Uncategorized” category but the_category() does not display anything.
My pseudo code inside The Loop:
$category_count = 0; foreach((get_the_category()) as $category) { $category_count++; } if ($category_count) { ?> | Published in <?php the_category(', '); } ?>
PS If there is a way to determine whether the item in the loop is a post or a page then I could skip the get_the_category() check. I didn’t see any functions for this in the Template Tags codex page.
- The topic ‘get_the_category returns category for a page’ is closed to new replies.