WordPress.org

Ready to get started?Download WordPress

Forums

Determine Cateogry for Header Image in Single.php (4 posts)

  1. sarahleo
    Member
    Posted 8 years ago #

    I have read and implemented Category templates to display various header images. But I want to display the corresponding header image for the category for a single post.

    Since Is_Category() does not work with single.php, how do you determine the category of a post outside the loop so that I can apply the appropriate sytle sheet that has the correct header image?

    Tx...... Leo

  2. Kafkaesqui

    Posted 8 years ago #

    This should provide the category ID outside The Loop on a single post page:

    <?php global $post;
    $categories = get_the_category();
    foreach($categories as $category) {
    $cat = $category[0]->cat_ID;
    } ?>

    $cat = $category[0]->cat_ID;
    This retrieves the first category ID available in the foreach, assigning it to $cat.

    $cat = $category->cat_ID;
    Change the above line to this to grab the last category ID in the foreach loop.

  3. sarahleo
    Member
    Posted 8 years ago #

    Great! Just what I needed!

  4. sarahleo
    Member
    Posted 8 years ago #

    Oops! Spoke too soon. It did not work because get_the_category() is required to be in the loop.

    Anyway, I found the answer in the post below:
    http://wordpress.org/support/topic/27073

    ... Leo

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.