WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] get_the_category outside the loop (3 posts)

  1. snakerboy
    Member
    Posted 8 years ago #

    I would like to use get_the_category function outside of the loop. Anyone got ideas how I might do this?

  2. skippy
    Member
    Posted 8 years ago #

    get_the_category() expects to be called from within The Loop. To use this tag outside the loop, you'll need to make a global post object named $post that contains the data of the post you want.

    <?php
    $id = 15; // which post ID to use
    $post= get_post($id); // fetch that post
    $cats = get_the_category();
    ?>

  3. snakerboy
    Member
    Posted 8 years ago #

    Thanks.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags