Support » Themes and Templates » Determine Cateogry for Header Image in Single.php

  • 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

Viewing 3 replies - 1 through 3 (of 3 total)
  • 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.

    Great! Just what I needed!

    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:

    … Leo

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Determine Cateogry for Header Image in Single.php’ is closed to new replies.