WordPress.org

Support

Support » Miscellaneous » header image by category

header image by category

  • 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”>
    </div>

    Thanks.

Viewing 5 replies - 1 through 5 (of 5 total)
  • mmm you alsa can create a new template called category-#.php where # is the category ID, so each category view can be totally different.

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

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

    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.

    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();?>

    to

    <?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 ?

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘header image by category’ is closed to new replies.