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

  1. sarahleo
    Posted 10 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 10 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
    Posted 10 years ago #

    Great! Just what I needed!

  4. sarahleo
    Posted 10 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:

    ... Leo

Topic Closed

This topic has been closed to new replies.

About this Topic


No tags yet.