WordPress.org

Forums

[resolved] Display different header per category that post is in. (4 posts)

  1. bencharity
    Member
    Posted 5 years ago #

    Ok I am trying to have a custom header be displayed when viewing a post within certain categories.

    I seem to only find instructions on how to display custom headers on the actual category pages.

    I have tried this code with no luck:

    <?php
    if ( is_category('7') ) :
      get_header('freelance');
    elseif ( is_category('17') ) :
      get_header('freelance');
    elseif ( is_category('18') ) :
      get_header('freelance');
    elseif ( is_category('19') ) :
      get_header('freelance');
    elseif ( is_category('20') ) :
      get_header('freelance');
    elseif ( is_category('21') ) :
      get_header('freelance');
    elseif ( is_category('22') ) :
      get_header('freelance');
    elseif ( is_category('23') ) :
      get_header('freelance');
    elseif ( is_category('24') ) :
      get_header('freelance');
    else :
      get_header();
    endif;
    ?>

    I have also tried the same code with the slugs:

    <?php
    if ( is_category('support') ) :
      get_header('freelance');
    elseif ( is_category('domain') ) :
      get_header('freelance');
    elseif ( is_category('email') ) :
      get_header('freelance');
    elseif ( is_category('file-management') ) :
      get_header('freelance');
    elseif ( is_category('godaddy') ) :
      get_header('freelance');
    elseif ( is_category('justhost') ) :
      get_header('freelance');
    elseif ( is_category('wordpress') ) :
      get_header('freelance');
    elseif ( is_category('xhtml-css-php') ) :
      get_header('freelance');
    elseif ( is_category('troubleshooting') ) :
      get_header('freelance');
    else :
      get_header();
    endif;
    ?>

    Am I simply doing something wrong? Or does this not work at all?

    Thanks in advance!

  2. stvwlf
    Member
    Posted 5 years ago #

    if this header is a different image for each category, I suggest you do it by displaying the images a background images in the header area. Then assign a class to the body class for each header that's supposed to display.

    It would all be handled by CSS.

    .support_hdr #header { background-image: url(images/support_hdr.jpg); }
    .troubleshooting_hdr #header{ background-image: url(images/troubleshooting_hdr.jpg); }

    etc etc

    in your header.php file

    if (is_category('support') {
       $body_class = 'support_hdr';
    elseif (is_category('troubleshooting') {
       $body_class = 'troubleshooting_hdr';
    }

    <body<?php echo ' class="', $body_class, '"'; ?>>

    If you're not following, ask.

  3. bencharity
    Member
    Posted 5 years ago #

    Ahhh I didn't even think of doing it that way!! Thanks stvwlf! I still have so much to learn!

    I finally got it to work using:

    <?php
    $post = $wp_query->post;
    if ( in_category('7') ) {
       include(TEMPLATEPATH . '/header-freelance.php');
    } else {
       include(TEMPLATEPATH . '/header.php');
    }
    ?>

    But I think that I will go back and implement your suggestion. I like that process better. Thanks again!!

  4. portlandridge
    Member
    Posted 4 years ago #

    bencharity,
    I like what you did here but I can't seem to get it to work for me. I have the header files created. Can you see if this code looks right?

    <?php
    $post = $wp_query->post;
    if ( in_category('4') ) {
    include(TEMPLATEPATH . '/header-seattle.php');
    if ( in_category('5') ) {
    include(TEMPLATEPATH . '/header-portland.php');
    if ( in_category('6') ) {
    include(TEMPLATEPATH . '/header-sf.php');
    if ( in_category('7') ) {
    include(TEMPLATEPATH . '/header-la.php');
    if ( in_category('8') ) {
    include(TEMPLATEPATH . '/header-sd.php');
    }
    else {
    include(TEMPLATEPATH . '/header.php');
    }
    ?>

    It works fine if I only have the first one like this
    <?php
    $post = $wp_query->post;
    if ( in_category('4') ) {
    include(TEMPLATEPATH . '/header-seattle.php');
    }
    else {
    include(TEMPLATEPATH . '/header.php');
    }
    ?>

    As soon as I repeat the code and add in all the other headers it only shows a white screen.

    Please help!

Topic Closed

This topic has been closed to new replies.

About this Topic