Genesis: 2 Ways To Add A Unique Header Image If Post In Specific Category

There’s many different methods you can choose from to display a different header image on all posts in a specific category.

In this post, i’ll show you 2 ways.

  1. The 1st is a custom function which pulls a unique header image from your child themes images folder and uses a conditional tag to display it on one specific category archive page as well as all posts within the same category. It then displays a default image for all other pages and posts.
  2. The 2nd method uses the template_include function in your child theme conditionally to display a custom header template for a specific category as wel as single posts assigned to that category.

Solution 1

Simply copy this PHP from the view raw link in the Gist and paste it at the end of your child themes functions file. Then you can add your images to your child themes images folder making sure they match the same file path, name and extension as whats in the code.

Solution 2

This code displays a unique header image on all posts in one specific category as well as the category archive page for the same category.

You can easily modify the conditional tag if you only want the unique header image to display either on the category page or only on posts in one specific category.

There’s 3 steps needed to make this work:

  1. Add a default header image
  2. Create a new custom header file
  3. Use a custom function to include the new custom header file conditionally

Step 1

Add your default header by going to Appearance > Header > Custom Header feature. You may need to modify the values for width and height in your child themes functions file. Here’s an example of what the code looks like:

//* Add support for custom header
add_theme_support( 'custom-header', array(
    'width'         => 1140,
    'height'        => 200,
    'header-selector'   => '.site-header .title-area',
    'header-text'       => false
) );

Step 2

  • Create a new file named custom-header.php.
  • Copy the PHP code from the view raw link in the 1st Gist labelled custom-header.php and paste it into the new file which you can then upload to your child themes root directory.

Make sure the path, file name and extension to the header image in your child themes images folder matches whats in the code.

Step 3

Copy the PHP code in the Gist above labelled functions.php and paste it at the end of your child themes functions.hp file using a text editor.

Make sure you replace the slug in the conditional tag with the category or I.D slug of your own.

Style.css File Tweaks

Note: You may also need to change the width and height values under the Site Header section of your child themes style.css file to match what you use in your functions file for the custom header function.

Also look for the height and width values for the .header-image .site-title a classes.

Notes

  • Tested on the Executive Pro child theme by StudioPress and may need tweaking for use on other themes

Also check out plugins like WP Display Header and Justin Tadlocks Custom Header Extended plugin.

Related Tutorials


Comments

8 responses to “Genesis: 2 Ways To Add A Unique Header Image If Post In Specific Category”

  1. Zimbrul Avatar

    I was wondering if the above code works for pages, too.
    I’d like to add different headers for different pages either as header above the title or background for the page title and to be able to choose the header from within the page editor.

    1. Brad Dalton Avatar
      Brad Dalton

      I think in your case you may want to test a plugin like WP Display Header or the Custom Header Extended plugin by Justin Tadlock.

  2. Hi Brad,
    Your notes are super helpful, but I’m trying to do something a little different which is switching out the header image used with the tag

    .site-title a

    My problem is I have a logo that looks good when left justified, but when the nav gets centered below 768px, the logo image needs to switch to a centered image. I’ve tried customizing the css using !important and also injecting code using the wp_head() hook. It doesn’t stick. I’m using Parallax child theme, but this seems to be an issue with the WP or Genesis header image customization tool which injects style setting just before the tag and overrides my css or the injected wp_head() hook code.

    I’ll share the site for a few hours in case you are online so you can see. I have to cover it up soon because my clients don’t want it out there yet.

    The code I’ve injected using the wp_head() hook is this:

    .site-title a { background: url(images/logo-360-tall.jpg) no-repeat !important; }

    It’s being overridden by what I assume is injected by the header customization.
    .site-title a { background: url(images/logo-brooklyn.jpg) no-repeat !important; }

    1. Brad Dalton Avatar
      Brad Dalton

      Hello David

      This question is not really related to this post however it seems you fixed the issue by removing the PHP code from the functions file which adds support for a custom header image under Appearance > Header.

  3. Zimbrul Avatar

    Brad, great tutorial. I was looking for something like that as I need to create a site with “Services” and I want each service page to have a different header image to illustrate the service.
    I think would be a good idea to show some screenshots of what the tutorial is all about. This will help Genesis beginners to understand better the output of the solution.

    1. Brad Dalton Avatar
      Brad Dalton

      Hello Zimbrul

      Yes i did think about creating a screen cast but my audio hardware isn’t great at the moment.

      I will add one showing the result. Was going to also show how and where to add the code but don’t think that’s necessary, what do you think?

      1. Zimbrul Avatar

        On many posts is not necessary but some will benefit from showing some screenshots.

        1. Brad Dalton Avatar
          Brad Dalton

          Yes.

          I think a screencast for this is best so i will make one but not sure what people will think it it doesn’t have any audio?

Leave a Reply

Join 5000+ Followers

Get The Latest Free & Premium Tutorials Delivered The Second They’re Published.