Twenty Eleven Child Theme: Custom Header

In a previous post about creating a child theme for Twenty-Eleven, we created a custom CSS file using the @import CSS rule.  Technically, the new CSS file is all that is needed to create a child theme in WordPress, but just changing the colors and font styles isn’t that much fun.  If it’s worth making your own WordPress child theme, it’s worth changing everything…starting with creating a custom header file.

Creating a custom header file for our child theme

Creating a custom header file in WordPress is even simpler than creating a custom style sheet, because this time we’ll just copy the file directly from the base Twenty-Eleven theme directly into our child theme.

  1. Go to your “/wp-content/themes/twentyeleven” directory and locate the file header.php
  2. Paste the header.php file to your “/wp-content/themes/twentyeleven-child” directory

That’s it!  Not very exciting…so let’s do some additional code modification while we’re in the new file.


Changing header image and size

Of all of the great things about the WordPress Twenty-Eleven theme, the one thing I can’t understand is why WordPress decided to make the header image so damn big!  There is the option to change the image you display to any number of provided images, or upload your own as part of the Appearance > Header menu.  But you can’t change the size…if your upload is too short, the theme will stretch it for you, distorting the image.  If your upload is too large, you’ll have to crop it. Luckily, with a minor modification we can change that.

First, go your newly created header.php file and find the following code:
What we’re going to do is use the commenting tag in HTML to “comment out” the portions of our header code that we no longer want to be active (i.e. the re-sizing feature in the Twenty-Eleven theme).  We can do this by adding the following tag (a left arrow, and exclamation point, and two dashes):


You’re not required to put in the “Comment out WordPress header checking” text as I did, but it is good practice to notate why changes have been made to your code.  A little documentation now goes a long way a year from now, when you can’t remember why you changed something! Now that we’ve got the opening tag for commenting, we need to find the end of the code we want to comment out, and put the closing tag.  Once we do this, all of the code between the commenting tags will not get executed by our browser.


The closing tag above is the two dashes, then the right arrow.  Again, you don’t have to write “End of RZ commenting out”, but it’s good for documentation.  Save your header.php file to make the completed changes.

Hey, now I don’t have ANY banner!

If you refreshed your blog at this point, you’ll realize that not only did we get rid of the massive size of the banner, we got rid of the banner overall!  That’s not the goal of this tutorial…so let’s add the banner back, except now we’ll control the sizing.

First thing you need to do is make sure you have the banner you want uploaded to your server, and that it is the size you want.  This blog is 1000 pixels wide at the body (white space), and I decided to make my banner 225 pixels tall, because that’s where the picture looked good.

Once you have your banner location, we need to add the HTML link to our image, using the image tag.  For the “alt” section, you should put a description of what your banner represents, as this will be displayed if the users browser can’t download the image.  In the “src” section, add the location of your banner file.


Once you hit save, that’s it!  You now have control over your banner image.  Yes, modifying the code in this manner disables the Appearance > Header functionality in the theme, but that’s a small price to pay to get flexibility.  If you want to change your banner in the future to show a different image, you’ll need to do so in the header.php file.  But since you’ve documented this change in your code, you’ll know where to look and what to change when that time comes…

Before – 1000px by 288px image

After: 1000px by 225 px

25 comments
Buishi
Buishi

Is there a way to only modify part of the header, possibly using functions.php, so that when the theme needs to be updated, it doesn't use the old header?

randyzwitch
randyzwitch moderator

 @Buishi Whether you update the header file or the functions file, unless you create a child theme, any updates to the theme are going to overwrite the modifications you've made.

 

Making a child theme is pretty easy, at minimum you just need to create a new directory and CSS file:

 

http://randyzwitch.com/twenty-eleven-child-theme-creating-css-file/

 

Once you've created your child theme, you can copy any of the files from the 'parent' theme into your child theme (so, functions.php, header.php or anything else), and when the theme is updated, your theme will refer to the files you've modified, preserving any changes.

Buishi
Buishi

 @randyzwitch  @Buishi My question was more "can I modify parts of the header FROM functions.php IN the child theme, which I've since found out I can't, I need to put header.php into the child theme folder.

gster
gster

I need some help understanding the second half of the above post. I have pretty good experience, changing code on my website in Cpanel.  But still a novice. I wanna change the header, but I dont understand where to insert code at "alt" ..or src section? HuH?  Im using the latest ver 3.5 of WP. Thanks

randyzwitch
randyzwitch moderator

 @gster Sorry about that...when I moved to a new theme, it doesn't look like the code is formatting correctly.

 

 

There are two parts to this tutorial:  1)  Disabling the massive default header image that comes with the Twenty Eleven theme.  That is done by commenting out the image header code in the header.php file.  In order to make sure you don't delete anything while you test this out, you use the HTML comment codes to just "hide" the code from being executed, rather than deleting the code outright.  This is what's done in the "Changing header image and size" section of this post.

 

2)  Once you comment out the default WordPress header code, you won't have an image at all; this is expected.  What you need to do is find a header image of the width/height that you want and upload it to your server somewhere.  Once you have the location of your file, then you add the following line of code directly under the code you commented out in Part 1.

 

<img src="http://randyzwitch.com/wp-content/themes/twentyeleven-child/images/butlers-wharf.png" alt="randyzwitch.com header" />

 

The "src=" part is the location of your file; you use the "alt=" part of the code to make sure text displays on your site just in case the image doesn't load for some reason.

 

Hope this helps.

Latest blog post: safecss

tomatogurl86
tomatogurl86

i not really understand your tutorial..can u be specific about the code?

randyzwitch
randyzwitch moderator

 @tomatogurl86 Could you be more specific about what problems you are running into?  Or, what you're looking to accomplish?

mondoleal
mondoleal

 @randyzwitch  I'm stuck on the html part. Where does it go? I'm not savvy with the php stuff. I got the banner to disappear. I uploaded my image on the server. Now where does this img tag go? In the header.php file?

NazaAlkhateeb
NazaAlkhateeb

Hey !

I had the code for a customized image rotator, I just didn't know how to put it in place of the twenty eleven header. The first part of this post helped me out alot.

Thanks for that

kennethbsmith
kennethbsmith

Cool beans! What if I want a specific header image per page? ie, BIO
Is childhood photo; SPORT is running photo; CONTACT is on-th- telephone photo; etc. Some sort of if/then statement?

randyzwitch
randyzwitch moderator

Hi Kenneth -

If you want to have a specific header per page, you need to know the page number of each of your pages. You can find this by going to "All posts" or "All Pages", then hovering over the "Edit" link. At the bottom of your browser, it should show you the full link, and within the link you'll see actual number (it's the number after post= ).

For example, my "About" page is page 472...if I wanted to put a specific header picture on just that page, I could put the following code in header.php:

<?php

if (is_page( '472' ) ) {

echo "<img src='http://randyzwitch.com/wp-content/themes/twentyeleven-child/images/randy-zwitch-logo.png' alt='randy zwitch logo' class='headerlogo' />" ;

} elseif (is_page( '519' ) ) {

echo "<img src='http://randyzwitch.com/wp-content/themes/twentyeleven-child/images/randy-zwitch-logo.png' alt='randy zwitch logo' class='headerlogo' />" ;

} else {

echo "<img src='http://randyzwitch.com/wp-content/themes/twentyeleven-child/images/randy-zwitch-logo.png' alt='randy zwitch logo' class='headerlogo' />" ;

}

?>

The "echo" statement puts the code on the page when the IF criteria is true...otherwise, nothing will get output in your header. Be sure that you use double-quotes on the outside of your string, and single quotes on the inside of your string when you need them.

The "elseif" statement lets you keep picking different pages, and the final "else" statement lets you assign a header to every other page that isn't specified in your code.

Does that help?

StevieWilliams
StevieWilliams

 @randyzwitch Hi Randy, I'm contacting you from overseas, about your custom header code instructions above from a year ago. Is this code still valid? I've follow your instructions from top to bottom! The only thing that is showing in the header of the site is the "alt tag" information. I'm working at home with MAMP installed on the computer I'm using. Can you please help find the problem, why the image isn't showing at al! lHere is an example of the beginning of the code:

<?php

if (is_page( '224' ) ) {

echo "<img src='http://billygoathair/wp-content/themes/capriolenhof/images/hof.png' alt='Der Capriolenhof' class='willkommen' />" ;

 

Thanks in advance,

Stevie

 

 

StevieWilliams
StevieWilliams

 @randyzwitch  @StevieWilliams Hi RandyZwitch, it's me again, Stevie!

First of thanks for replying to my request for help;-) Also thanks for your explanation on the source link problem, that has been resolved now! I still have a problem with the blog link image:

} elseif (is_page( '28' ) ) {

echo "<img src='http://localhost:8888/billygoathair/wp-content/themes/capriolenhof/images/blog.png' alt='Unsere blog - Der Capriolenhof' class='blog' />" ;

 

What I would like to have, is that when someone clicks on the link button "Blog" it goes the the post page, but displays a header image. Is there a way set the image on blog post?

Thanks again for all your great help, it's been a blessing!

StevieWilliams

randyzwitch
randyzwitch moderator

 @StevieWilliams Hi Stevie, thanks for stopping by.

 

If only the Alt tag is showing, it means that the image is not available to be displayed in the browser.  In this case, when I click on your src= link, I get a message that says "Server Not Found".  So it may be the case that you're not linking to a publicly available image

 

What you can do to validate whether your code is correct is link to a publicly available image.  Substitute the code below into your header file, and see if it returns an image.  If so, then your problem is with the image you are linking to in your original code. 

 

echo "<img src='http://randyzwitch.com/wp-content/themes/twentyeleven-child/images/butlers-wharf.png' alt='Der Capriolenhof' class='willkommen' />" ;

 

If that doesn't work, let me know and we can try something else.

kennethbsmith
kennethbsmith

Big help, yes. Thanks.

But, a couple of things. I see from you example that you use TwentyEleven. I am doing this to child theme of TwnetyEleven. Adding your code puts the header photo there but beneath the rotating header photo. So it seems I need to remove or comment out something.

I tried but my PHP skillz are not what my CSS skillz are. (But sooon!)

Suggestions?

Here is the site: http://www.erichsegal.com (I'll take it out of Maintenance Mode)

(I had done this with Rotating Header Images but now they want to see it with a fixed image per page.)

I have only done four pages so far: Academia, Film&Music, Bio, and Sport.

Note that the Bio page looses the CSS below the header photo.

Bio is the first in the statement:

<?php

if (is_page( '5' ) ) {echo "<img src='http://www.erichsegal.com/wp-content/header-images/pers036.jpg' alt='Brooklyn Stoop logo' class='headerimage />" ;

} elseif (is_page( '8' ) ) {echo "<img src='http://www.erichsegal.com/wp-content/header-images/sport034.jpg' alt='jogging' class='headerimage' />" ;

} elseif (is_page( '14' ) ) {

yadda yadda ?>

Got no idea why the CSS is lost. Do you?

kennethbsmith
kennethbsmith

@randyzwitch Randy- I just deleted my comments above with URLs in them. Today I got a call from the client who was doing a search and thought this was confusing and asked if I could remove it. Not sure if that will work with LiveFyre. But thought I would tell you why I deleted some of my comments.

kennethbsmith
kennethbsmith

I thought about it and double-checked my code. Yup, I had dropped an apostrophe.

This has been great.

Many thanks.

kennethbsmith
kennethbsmith

Oppps. I see you address the commenting out in the tutorial. My bad. Let me follow those instrux. brb