Tutorials

Page specific headers in Thesis

28 August 2009 · 58 comments · tagged with , , , , ,

I’ve done several posts about Thesis headers before: Thesis full-width headers, Full-width headers in Thesis 1.5 and CSS Custom Headers.

All of those lead you in the same general direction: a single custom header for your blog. I recently got asked for step-by-step instructions on how to specify different headers on specific pages. So, I’m going to show you two ways to achieve that: CSS image replacement and inline header images.

CSS image replacement

The simplest way to customise your header (in my opinion) is to use a background image. There are several permutations of the CSS1 that will achieve such a thing, but the bare minimum would be something like the following.

.custom #header {background:url(images/header.jpg) center top no-repeat; height:150px; padding:0 1.1em;}
	.custom #header #logo a {display:block; height:150px; text-indent:-9999px;}

If header.jpg is in your custom/images folder and you’ve unchecked the option to display the tagline on the Thesis Options page, the above CSS will give you a clickable header that looks like…

Header image using CSS replacement

Header image using CSS replacement

This will be the header for every page that we don’t specifically change.

Page specific body class

Thesis provides body classes for all the pages in your WordPress site. You can also provide your own body class to any page or post by filling in the relevant field when writing a post. On top of all that you can filter the body class via a custom function to add a body class to any page accessible via conditional tags.

Source code showing page specific body class in Thesis

Source code showing page specific body class in Thesis

Add your own body class to a Thesis page or post

Add your own body class to a Thesis page or post

Page specific header

Using the page specific body classes, we can give individual pages their own CSS header. All you need to do is use .page-name instead of .custom.

Assuming the new image is the same height as the default one, the CSS to give your About page a unique header is…

.about #header {background:url(images/header_about.jpg) center top no-repeat;}
Unique header on the About page in Thesis

Unique header on the About page in Thesis

Adding body classes

You can keep adding unique headers across your site to your heart’s content… If the page you want to target doesn’t already have a unique body class you can give it one with the following function.

function custom_body_classes($classes) {
if (is_home())
	$classes[] = "home";
if (is_category())
	$classes[] = "category";

	return $classes;
}
add_filter('thesis_body_classes','custom_body_classes');

This function (placed in custom_functions.php) specifies the pages to target using conditional tags, and then adds the desired classes to an array called $classes, which is filtered back through the Thesis function that outputs the body classes2. Using this we could give the same header to all the category archive pages (with is_category()), or all the posts in a specific category (with in_category('Fancy Headers') and so on.

CSS summary

All of the CSS put together might look like the following.

.custom #header {background:url(images/header.jpg) center top no-repeat; height:150px; padding:0 1.1em;}
.about #header {background:url(images/header_about.jpg) center top no-repeat;}
.archives #header {background:url(images/header_archives.jpg) center top no-repeat;}
.category #header {background:url(images/header_category.jpg) center top no-repeat;}

	.custom #header #logo a {display:block; height:150px; text-indent:-9999px;}

Inline header images

Alternatively, we can use PHP and HTML instead of CSS to replace the contents of the header.

The whole function that goes in custom_functions.php is,

function custom_header() {
	// header for About page
	if ( is_page('About') )
		echo '<p id="logo"><a href="' . get_bloginfo('url') . '"><img src="' . THESIS_CUSTOM_FOLDER . '"/images/header_about.jpg" alt="' . get_bloginfo('name') . '" /></a></p>';

	// header for Photos category pages
	elseif ( is_category('Photos') )
		echo '<p id="logo"><a href="' . get_bloginfo('url') . '"><img src="' . THESIS_CUSTOM_FOLDER . '"/images/header_purple.jpg" alt="' . get_bloginfo('name') . '" /></a></p>';

	// header for posts in Tutorials Category
	elseif ( in_category('Tutorials') )
		echo '<p id="logo"><a href="' . get_bloginfo('url') . '"><img src="' . THESIS_CUSTOM_FOLDER . '"/images/header_tutorials.jpg" alt="' . get_bloginfo('name') . '" /></a></p>';

	// header for all other pages
	else
		echo '<p id="logo"><a href="' . get_bloginfo('url') . '"><img src="' . THESIS_CUSTOM_FOLDER . '"/images/header.jpg" alt="' . get_bloginfo('name') . '" /></a></p>';
}
remove_action('thesis_hook_header', 'thesis_default_header');
add_action('thesis_hook_header', 'custom_header');

Understanding the function

  • The only parts that need changing are the conditional tags and the image names.
  • There is a list of conditional tags in the WordPress Codex that can be used in this function.
  • In the HTML markup for the header I’ve included <p id="logo"> to match the default Thesis markup.
  • get_bloginfo('url') is PHP and fetches the URL of your blog.
  • get_bloginfo('name') is PHP and gets the name of your blog. This is important as the alternative text for the image; if the image doesn’t load for any reason the alternative text will be shown and since we wrapped the image with <p id="logo"> it will be formatted correctly as the blog title.
  • remove_action('thesis_hook_header', 'thesis_default_header'); removes the default markup of the Thesis header so that we can add our own content into the header.

If you want to insert this code using the Thesis OpenHook plugin, make sure that you convert the function properly and don’t forget <?php ?> tags around the conditional tags.

Best of luck and feel free to ask questions in the comments below!

  1. If you want more info on basic CSS elements and syntax, have a look at the Tizag CSS tutorial []
  2. For more about PHP syntax and operations check out the Tizag PHP tutorial []

{ 52 comments… read them below or add one }

1 Tom August 28, 2009 at 23:10

This is WONDERFUL. Thank you!
Could you maybe share a note about how to size/create your own header to begin?

Thank you again for your sharing.
T

2 John O. August 29, 2009 at 02:54

Kristarella,

This works for me. I used the CSS Image Replacement method. Thanks again so much!

John

3 shawn August 29, 2009 at 03:57

Thank you for yet another wonderful tutorial. One of the things I really like about visiting here each day, is I not only find out something new I can do to my site, but how to do it. Very well done!

4 SeekingFF August 29, 2009 at 07:17

Kris – You always do such a good job with your tutorials. I still go back and re-read some of your earlier one’s here and on the Thesis forums whenever I need a refresher. Another job well done!

Chris

5 Ashwin August 29, 2009 at 16:26

Kris – another useful post. Thanks for sharing.

I have problems in making my Thesis header image clickable (URL will be my blog url). Can you help me out? You can visit my blog – I am providing it in the website link.

Thanks

6 kristarella August 29, 2009 at 17:48

Ashwin — you need the CSS .custom #header #logo a {display:block;} (because by using a negative text indent there is no text to fill out the inline anchor element in the header).

7 Ashwin August 30, 2009 at 01:13

Kris – that worked well. Thanks for the tip.

I follow a lot of your tutorials. Would be great if you can give me some feedback about my blog design using Thesis. Thanks for your time.

Regards,
Ashwin

8 Mark August 30, 2009 at 12:55

Hey Kristarella – Over the past week, I’ve read your blog posts on custom header so many times, I feel like we know each other.

I’m waiting for the penny to drop via experimentation. Backing Tom up – creating the headers in Photoshop – is there any particular size I should be sticking to? I’m thinking a similar size and layout to surgarae’s header with the background all the way to the top.

I’m using Firebug to see whats going on there but I’m stumbling around in the dark still. It will come.

Thanks again so much for the time you have taken to write these posts.

Mark

9 kristarella August 30, 2009 at 13:26

Tom & Mark — Since users can make their Thesis site absolutely any size they want it’s impossible to say what size your image should be, except that Mark has the right idea with using Firebug.

I have a video about using Firebug and you can also use Webkit Inspector if you’re in Safari or Chrome. By clicking on “Layout” in Firebug or “Computed Styles” in the Webkit Inspector you can determine how wide in pixels your header is. I think in a default install of Thesis the header is 927px wide plus 11px of padding on each side, so your header should be 949px wide.

You can use whatever height you want. I used 150px, Sugarrae’s is 215px including the red line below it (although the image is slightly wider than the header actually is).

10 Mark August 30, 2009 at 16:39

Thanks for the prompt reply Kristarella,

That video tutorial was off the ricta!

I get it now – sorta. In firebug – I checked the layout tab, inspected my header (and sugarae’s – firebug is borderline creepy) and the layout told me exact sizes. No more use for the Measurit addon.

My header area is 1664 x 189. Should I just create my photoshop logo that size. Or can I easily adjust the dimensions of my header area?

Last question – is Sugarrea using a full width header? I notice her background appears on both sides of her logo. I like it.

Promise I won’t hassle you any more. :-)

Mark

11 Mark August 30, 2009 at 17:08

OK – update – your video on Full Width Framework just answered my ‘last question’

12 Tom August 31, 2009 at 00:22

Thank you so Much Kris.
Tom

13 kristarella August 31, 2009 at 11:57

Mark — glad the full-width video helped, but I’m not really sure about your first question. #header_area will be however big your browser is, so I would recommend a solid colour or a repeating image or texture for that. The main content part of your header with title etc needs to be the width of #header or smaller, otherwise people using smaller monitors won’t be able to see all of it.
Hope that helps!

14 Terry Breedlove September 9, 2009 at 02:45

When building a custom header for a thesis theme is the best size 900 by 150? Or is there another size that works better.

Thanks

Terry

15 kristarella September 9, 2009 at 09:40

Terry — Please see the comment above regarding image sizes. Thanks :-)

16 Steve September 23, 2009 at 06:13

Kristarella,
You are a genius. I have been struggling with this for 2 days. What a simple solution!

17 Michael Cowen October 9, 2009 at 00:45

Hi Kristella,

Thaks for this. I have been pulling my hair out all day trying to customise my headers. Just a quick question. I have made my home page a static page and put my posts on my blog page. when i use your css route above and use .blog #header … it picks up the .customer #header. Do you have a suggestion on how i can fix this??

Thanks
Michael

18 kristarella October 12, 2009 at 10:57

Michael — Every page will always have the “custom” class assigned to the body and certain pages will have additional classes assigned. .custom and .home always have the same importance because they are both only classes, so the only way to override a style on the same element is to put it after the other style in the style sheet. So you should put all your default custom styles first and then specialised page styles later in custom.css.

19 Michael October 12, 2009 at 17:30

Thanks so much Kristarella. Really appreciate the help.

20 Anshoo October 24, 2009 at 14:24

Hi Kristella,

First of all, your website is fantastic. I learned a lot from it.

However, I had a question, which I’m sure is very simple for you to answer.

Is there a way I can have my content container be white whereas my entire site background be the same color as my footer (#444444)? I have been struggling to figure this out for several hours now, but haven’t been able to find a solution.

I hope you will be able to help me.

Thanks in advance. – Anshoo

21 Anshoo October 24, 2009 at 14:26

I forgot to mention that, I have been successful in doing so with both Firefox and Chrome, but I haven’t been able to figure out how I can implement this with IE 6. Most of my user-base uses IE6.. :(

22 kristarella October 25, 2009 at 09:37

Anshoo — It is not working 100% in IE6 because IE6 hates negative margins. You would be much better off using the full-width framework.

23 Anshoo October 25, 2009 at 09:43

Hi Kristarella, I played around nearly the entire day today and all of last night.. and was able to figure out the reason. I’m 99% done :)

Just one question though, if you see the website, the border above the copyright stretches all the way across the screen (in FF). Is there a fix for that? IE is nearly done, too. GodHammer and DiyThemes helped me out a lot :)

Thank you :)

24 kristarella October 25, 2009 at 09:53

Ansho0 — The width of that section is 97%. Try making it 97.4em like the page container.

25 Anshoo October 25, 2009 at 09:58

Thank you so much Kristarella! Its finally fixed now.

:)

26 Anshoo October 27, 2009 at 09:28

Kristarella, thanks to you, I think this is the first time I like the way my website is looking now.

27 Khan November 26, 2009 at 11:29

Hello Kristarella,
I’m trying to add a page specific header within my site.
I used the code for page specific header in my custom.css
.test #header {background:url(images/VS1CROPR.jpg) center top no-repeat;}

the image gets pulled, but it sits behind the default flash header for the rest of the site. Therefore you don’t see it because the flash header still runs on the page.
If you reload the page you will see the page specific header for a split second. So it is there, but behind the default header.
Do you know how I can fix this?
Thank you!!!

28 kristarella November 26, 2009 at 11:58

Khan — Since the flash header is probably added as HTML via PHP you will need to add an exception in the PHP in the form of a conditional tag. E.g. if (!is_page('Test')) { FLASH CODE }.

29 Khan November 27, 2009 at 01:44

Kristarella,
Thank you for the advice, I haven’t tried it as I’m just reading this now.
I solved the issue by using Dynamic Header Plugin
http://wordpress.org/extend/plugins/dynamic-headers/
Worked out okay with a bit of tweaking.
Thanks for the post and the great article.
Khan

30 Joel December 19, 2009 at 12:42

In ‘Inline header images’ section where you have ” . THESIS_CUSTOM_FOLDER . ” in the code, is that to be replaced with a path?

I want to use this in Openhook since I already have it displaying rotating random images on pages, but I want to use a specific image on a few special pages. Trying this, I get the alt text – the site title and not the image, so I’m not getting the path to the image right.

Thanks

31 kristarella December 28, 2009 at 10:45

Joel — It does not need to be replaced as long as you are echoing with PHP because THESIS_CUSTOM_FOLDER is a defined path in PHP for Thesis. If you’re using OpenHook and no longer echoing the URLs, but using plain HTML then you do need to edit the path so that it goes from src="' . THESIS_CUSTOM_FOLDER . '"/images/header_purple.jpg" to src="http://www.example.com/wp-content/themes/thesis/custom/images/header_purple.jpg".

32 Nancy L. Hoffmann January 21, 2010 at 09:54

I feel like an idiot! But I cannot get a header image to show on http://earthadvertising.com/blog.

I’ve put the EAheader.jpg in both the images file and the thesis/custom/images file; but it can’t seem to find it. You can see my css on this blog page. Here’s what I put in the Custom File Editor:
.custom #header {border-bottom:none; height:112px; background:url(images/EAheader.jpg) center left no-repeat; padding-top:0; padding-bottom:0; }
/* .custom #header #logo {display:none;} */
.custom #header #tagline {display:none;}

(I temporarily hid the .custom #header #logo info, because I had to have SOMEthing up there!)

Please help! I’m new to Thesis, and it seems rather awkward so far.

Many thanks!

— Nancy

33 Will January 22, 2010 at 03:44

Thanx for the great Thesis article. Came here through search and just couldn’t stop reading. Very good blog :-)

34 kristarella January 22, 2010 at 10:18

Nancy — Looks like you’ve changed the whole theme over. Sorry, can’t help if I can’t see the issue.

35 Claudia January 26, 2010 at 06:36

Hi Kristarella,

I used your CSS code for a custom header and also the one so I can have different headers on individual pages. On Internet explorer it works perfect, but on google chrome and firefox my headers do not show at all. Do you maybe know of a reason for this, and maybe a possible solution? Did I pherhaps do something wrong?
thank you so much

36 kristarella January 27, 2010 at 10:57

Claudia — It’s not a good idea to have file names with spaces in them. Try changing your image file names to have hyphens or underscores instead of hyphens. I don’t know if it makes any difference, but Thesis doesn’t need to be in an extra folder, you seem to have themes/thesis_16/thesis_16 All the theme files can just be in the first thesis_16 folder, it doesn’t need to be two-tiered, but it might not make a difference, I’m not sure.

37 Claudia January 28, 2010 at 06:48

Hi,

I removed all spaces from my file name and that did the trick. Thank you so much. I realy appriciate your advise and help. Your posts are a great support. thank you!!!!

38 Thesis Theme Design March 11, 2010 at 21:20

The articles you have provided above is really helpful and really useful for the Thesis Theme lovers. I would like to decorate my blogs header by the way you said. Good works kristarella.

39 Michelle April 6, 2010 at 14:50

What would I do if I want to make the after_header image to show only on the homepage?

40 kristarella April 6, 2010 at 15:14

Michelle — It depends how you have added it. If you’ve added it as HTML in a function, you wrap it in a conditional like this, if it’s a CSS thing you use a body class.

41 webdesign April 8, 2010 at 00:11

I studied the css tutorial and I really learned a lot. I think my next homepage will be a lot better than my first one.

42 Tim April 14, 2010 at 11:20

Thanks very much for this tutorial. I am doing a lot of customisation on a clients site and they wanted a page with no navigation in the header which freaked me out initially. I used the inline header method and can specifiy exactly which pages will have the different header now – yay : )

43 Will Reichard April 21, 2010 at 05:38

Thank you–tried a whole bunch of solutions to this, and this was by far the most elegant. Appreciate it!

44 Tony May 5, 2010 at 05:12

Can you please let me know how can I put the menu bar at the header height. For example I have my logo at the header to the left side of the page but I want to put the menu to the right side of the logo but at the same height as the header. What would the be to acquire that? Thanks in advance

45 kristarella May 5, 2010 at 10:08

Tony — Take a look at the menu CSS for my mini skin, which has the menu in line with the header. I may have to write a tutorial for it because I get a lot of questions about how to do it.

46 bhorton May 13, 2010 at 07:54

I have followed your tutorial, which all of them are great by the way, and I have run into a little tiny problem. I used the CSS way, giving each page a unique class and adding the different header via CSS, it seems that this works, going to about shows the about image, and going to contact shows contact, but it makes my menu disappear when I have the code (remove_action(‘thesis_hook_before_header’, ‘thesis_nav_menu’);
add_action(‘thesis_hook_after_header’, ‘thesis_nav_menu’); ) in my functions.php from a different tutorial on your site to make the navigation below the header. I would like my navigation to essentially float on top of this background header that I am trying to use. I thought using the function edits I pasted here would allow both things I want to work, but when adding this unique header, it causes the menu to disappear, but without it, my nav is on top of my header and will not overlap the image. Any suggestions? Thanks in advance!

47 kristarella May 13, 2010 at 10:00

Bhorton — CSS header images shouldn’t affect the navigation in anyway, unless there’s an error in your custom_functions.php file before the nav code that’s preventing it to execute. Perhaps If you paste your file at Pastebin and give the link, I might be able to see if that’s the case.

Or, you might just need to use thesis_hook_after_title instead of thesis_hook_after_header because the latter doesn’t put the nav inside the header element and would therefore be below any image you have in #header.

48 bhorton May 14, 2010 at 00:11

Using the thesis_hook_after_title rather than header worked and allowed me to put the navigation on top of the header, and it is still there on the other pages now! Thank you. – You do great work!

49 Joshua July 22, 2010 at 03:58

Hi Kristarella,

I want to to overlay about 20px of my header image onto Content. Cuz there is a little image that should present on the content which originally is a part of Header. Is there anyway to do that?

Thank you,
Josh

50 kristarella July 22, 2010 at 09:50

Joshua — It depends a bit on which framework you’re using etc, but you can probably achieve something like that with positioning. E.g., position:relative; top:-20px; to move something up on the page by 20px. You might also need to add z-index to some elements to get them to overlap in the right order.

I’ve got a bit more info about CSS position in my position nav tutorial.

51 Adam J. Blust July 23, 2010 at 05:01

I’m building a Thesis site with a lot of static pages, most with at least one level of subpage. I would like to have a separate header for each of these “sections,” or top level pages and the pages below it, and have the header appear automatically for each section – without the subpage creator having to do anything special. Is this possible?

Thanks for all your great tutorials.

52 kristarella July 23, 2010 at 23:16

Adam — Yes, you can use either method in this post to add the headers. Use the conditional tag is_page() to target the pages and a custom function to target the subpages. Details for that functions are on the conditional tags Codex page.

Leave a Comment

Want to show PHP or XHTML code in your comment? Encode it first so that WordPress doesn't strip it away.

Read the comment policy.

Previous post:

Next post: