header image by category (6 posts)

  1. schultzy
    Posted 8 years ago #

    I am looking at trying to get a different image to show based on the category of the post. I found this bit of code in the forums, but the topic is closed so I can't really ask questions on that post.

    This is the code I found.

    <?php /* Calls the header image for each category - necessary code */
    $post = $wp_query->post;
    if ( in_category('3') ) {
    $imgurl = "images/category3.jpg";
    } elseif ( in_category('4') ) {
    $imgurl = "images/category4.jpg";
    } elseif ( in_category('5') ) {
    $imgurl = "images/category5.jpg";
    } elseif ( in_category('8') ) {
    $imgurl = "images/category8.jpg";
    } elseif ( in_category('13') ) {
    $imgurl = "images/category13.jpg";
    } elseif ( in_category('15') ) {
    $imgurl = "images/category15.jpg";
    } else {
    $imgurl = "images/all.jpg";

    What I don't know is how to call the variable $imgurl to display the image.

    Here is what I to set up.
    <div id="header">
    <img src="$imgurl">


  2. oscarmota
    Posted 8 years ago #

    mmm you alsa can create a new template called category-#.php where # is the category ID, so each category view can be totally different.

  3. Kafkaesqui

    Posted 8 years ago #

    <img src="<?php echo $imgurl; ?>" />

  4. schultzy
    Posted 8 years ago #

    Thanks for the quick response. I will give it a shot this afternoon and report if it worked or not. Thanks again.

  5. schultzy
    Posted 8 years ago #

    It works as planned. Well, kind of. I still have to tweak some code to get placement right on my site, but the solution worked. Thanks.

  6. glennk
    Posted 8 years ago #

    Sorry to bring this one back up but its a similar problem I am having and I found this in a search.

    I have made a new template for a certain category and would like to call a differnt header image.

    I altered the code from

    <?php get_header();?>


    <?php get_header2();?>

    I was hoping to put a different image into header2 but it throws up an error. Obviously my knowledge of coding in limited if not non existant. Where have I gone wrong ?

Topic Closed

This topic has been closed to new replies.

About this Topic