WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] get_the_category returns category for a page (3 posts)

  1. daymobrew
    Member
    Posted 3 years ago #

    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.

  2. esmi
    Forum Moderator
    Posted 3 years ago #

    <?php if($post->post_type == 'post') :
    _e('Filed under:'); the_category();
    endif;?>
  3. daymobrew
    Member
    Posted 3 years ago #

    Perfect solution, thank you.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.