bioneural.net site preferences

Accessibility

Toggle width/ text size:

style

Default/Alternate

Suits visual impairment, mobile devices

Styling

Change the theme:

layout

NB: may reduce functionality

Link behaviour

Links with an icon are off-site:

links

Right-click any link to optionally open in a new window or tab


A custom header for one category

The post title says it all. I needed to change the header so that all posts in my WordPress blog falling under a certain category have a unique look. I'm new to PHP but prone to experiment, so jumped right in and found I could achieve this aim with just one line of code.

My original header.php read like this:

<div id="header">
...
</div>

The header div is styled in my external stylesheet with:

#header {
	background: black url(bioneural/neurons.jpg) center no-repeat;
	width: 100%;
	height: 224px;
	}

It renders this:

Neuron-Head

However, I wanted to replace the neuron background image with a koru, but only for posts in the Project Koru category (which includes the archive view of that category). I created an alternative header image with the same dimensions and uploaded it to my K2 sub-theme styles folder. I then edited header.php thus:

<div id="header" <php if ( in_category(15) ) { echo 'style="background: black url(../koru-header.jpg) center no-repeat;"';} ?>>
...
</div>

...where ".." is the full URL to the image (it apparently won't work with a relative URL) and "15" is the WordPress ID of the category to which I want the new rule to apply. Here we are making use of the hierarchical CSS cascade; the rule in our external stylesheet is over-ruled by the in-line rule we just wrote, replacing the "default" background image:

Koru-Head

A similar edit can be made to other header elements (name, description), sidebar elements, footer.php etc.

There may be other (better) ways to do this, but I think this is pretty simple. It works, and it validates.

Update 28.07.06: One tiny problem with the above. As soon as any post is in category 15, if will change the header including on the home page. I hadn't appreciated the difference between in_category (returns true if the current post is in the specified category id) and is_category (when any category archive page is being displayed). A slightly more complex rule will thus give us our unique header for both category and single pages in that category, without affecting pages featuring posts to multiple categories that include category 15:

( (is_category('15')) or ( (in_category('15')) && (is_single()) ) )

12 responses to “A custom header for one category”


  1. Comment 1 marc

    Great feature and quite fast implementation time ;-)

    Now tekoru.net is at least a little distinguishable from the rest (esp. the more tech related stuff).

  2. Comment 2 Bruce

    Vielen Danke Marc,

    I might apply the same trick to the blog title description, and hide (or swap) the band in the top right corner of the header. It may be possible to specify inclusion of different custom templates (e.g. header-default.php vs. header-koru.php) to help keep the variations seperate and easier to edit. I'll look into this when I get time.

  3. Comment 3 Aleister

    It is definitly possible :)

  4. Comment 4 Bruce

    Now that's teasing Aleister! The Master wishes the Student to work it out himself, huh? ;-)

    OK... do you think this would work for index.php?:

    <?php if ( in_category(15) ) { ?>
    <?php include (TEMPLATEPATH . '/header-koru.php'); ?>
    <?php } else { ?>
    <?php include (TEMPLATEPATH . '/header-default.php'); ?>
    <?php } ?>
    <div class="content">
    <div class="primary">
    <?php include (TEMPLATEPATH . '/theloop.php'); ?>
    </div>
    <?php get_sidebar(); ?>
    </div>
    <?php get_footer(); ?>

  5. Comment 5 Aleister

    Actually the in_category function has to be used within the loop, so that particular method will not work. WordPress will not know what category is being viewing until a post is loaded in the loop.

    Now if you wanted, you could insert the loop start code above everything else (not anything that outputs.. just the start of the while loop).

    Then you would be able to use in_category to determine what header to show.

    The main downside to that method is that you would only be able to use it in single.php for the actual post page, since it will only have one pass in the loop. If you did this to an archives page, it would go through the loop several times, trying to load the header, etc.. each time since it is after the start of the loop, and that would not work well at all.

    Now if you want to have the custom header for category pages as well, one method would be to grab the category name from the URL. :)

    I will be thinking about this, just in case there is a better option, but that would get it working for the cat listing pages as well as the single posts pages, which is all it would really need to be implemented for anyway, since archives pages etc.. will have posts from different categories together.

  6. Comment 6 Bruce

    Aleister, thanks. Use in_category within the loop you say? OK, I have in_category within header.php, which is called in index.php before theloop.php. I don't understand why my above trick works—unless it has something to do with the unique file structure of the K2 template...?

    You're right that to create a "site within a site" I would want just the single post pages and category pages to have a unique look—as I have above. So presumably what ever alternative method worked to include (say) header-default.php or header-koru.php would also work for two versions of archive.php which lists posts within category X (not to be confused with archives.php which lists posts by month and subject!)

    The Codex says:

    You might want to have different information or looks to specific category listings of posts. WordPress automatically looks for the category.php template file to generate the list of category posts, and if it isn't found, then generates them using the index.php. Using the template hierarchy, if it finds the category.php template file, it then looks for a category template file that matches the category ID number being sought, for example, category-2.php. If that template file matches, it will use that template file to generate a page view of all the posts in category 2.

    I don't have a category.php file—but maybe if I made one (i.e. category-15.php) and used include to load the correct header, footer, etc?

  7. Comment 7 Aleister

    Interesting. Well, I just looked again and noticed a clarification in the WP documentation.

    I have not ever tried using the category.php method, but that sounds like it would work nicely as well :)

  8. Comment 8 Olav

    Good stuff, and I really like the idea of chaning your site for each category. Thanks!

  9. Comment 9 Bruce

    Olav, a warm welcome to the Norwegian navy! I agree this is a useful facility, particularly if you want "site within a site" treatment for a single category. A unique header for a given category (e.g. Mac images for "Mac") should send a strong visual cue to readers regarding the page subject. However, I see two problems (there may be more!) if this is done for every category:

    1. Because visitors won't cache a re-usable header image as they browse, page load times will increase each time they browse a post in a new category.
    2. The "branding" value of a static header may be lost. I think the distinctiveness of the header is the main way I personally recognise/ recall a site as a returning visitor.

    There may be a compromise. At this point the best one I can think of is this:

    1. A smallish distinctive header/ site logo that appears on all pages.
    2. Use the CSS "z-index" property and positioning to layer a transparent category-specific .png on top of (or overlapping) the static header image.

    Would this not achieve the required balance?

  10. Comment 10 jez

    hi there,
    i really like the idea of changing the header, unluckyly i couldn't get it to work yet.
    have you?
    because by clicking through your site, I didnt notice a changing header so far.

    regards,
    jez

  11. Comment 11 Bruce

    Jez, yes, it is working. But you understand it's not a randomizer? It changes the header image for one category only. In my case, for this category. I use K2; I haven't tested it with a non-K2 WordPress installation. Remember you need to set the category to your ID, not mine (which is the 15 in the example here).

  12. Comment 12 stewar

    Hey thanks for the tip. I'm doing some pretty heavy category-based theming by using in_category within header.php to vary CSS style selectors that get output... but i couldn't work out why the supposedly default case wasn't being applied to the 404 page - something to do with being outside The Loop at that point i suspect - anyway your little bit of logic killed the 'bug' for me so thanks. (and thanks google for bringing me here :)

Something to say?

Comments may be moderated (e.g. no commercial promotion), are subject to spam filtering, and should be relevant to this post—otherwise make contact.

Usable tags include <a href=""> <blockquote> <em>. Select any text and click to quote.