WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Get multiple category names inside the loop (3 posts)

  1. applegateian
    Member
    Posted 9 months ago #

    I need to be able to get all the category names within a post in the loop.

    I have checked out WordPress get_the_category on this, and the example Show the First Category Name Only
    works how I want, but only shows One category:

    <?php
    $category = get_the_category();
    echo $category[0]->cat_name;
    ?>

    How do I get all of them?

    Thanks,

    Ian

  2. Scriptrunner (Doug Sparling)
    Member
    Posted 9 months ago #

    Your answer was right there in the Codex: Function Reference/get the category page under the "Show All Categories as Links" section :)

    Slightly modified/simplified:

    <?php
    $categories = get_the_category();
    if( $categories ) {
        foreach( $categories as $category ) {
            echo "$category->name<br />";
        }
    }
    ?>
  3. applegateian
    Member
    Posted 9 months ago #

    Worked perfectly, thanks.

Reply

You must log in to post.

About this Topic