Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In the developing category driven pages lesson you saw how to limit the posts and the content area to a specific category, but what about the other elements of the page like the header, sidebar, and footer? Can those change according to the chosen category as well? Well, the answer is you bet. In this lesson I'll show you two different techniques that work together to create custom headers by category. To illustrate these techniques, what we're going to do is swap out this background image that you see here.
So to start, I'll need to create a new header file that's dedicated to the Conference category. Let's expand the Files panel here in Dreamweaver, and in my roux theme folder I'm going to select header.php, and this time I'm just going to press Command+D which will duplicate the file. With a PC you can use Ctrl+D, and now I'm going to rename header-Copy, and I'll make that header-conference instead.
So let's customize our new header-conference.php file a bit. I'll open it up, close down the Files panel and switch to Code view, and what we're going to do is just add a new class to the header tag. So we have blogHeader and pageHeader so far. Let's add conferenceHeader. Now if we were working with a standard foreground image like the logo, we could just swap that out. But because it's a background image that's applied to this h1 tag here, we'll use CSS instead.
So let's save our page, and now we're going to need to call the new header page, and we want to call that from conference.php. So again, I'll just expand my Files panel for a moment so we can quickly access conference.php and then collapse it. Now you'll notice that the first real PHP directive here is get_header, rather than leave it at the generic get header we're going to specify, and we'll specify conference in single quotes. Notice that we don't specify header-conference.php or something like that, it's just conference.
You only need to put in the keyword and WordPress will do the rest of the work. Okay, we're ready to save this. That's the only change we need to make here. Now let's go to our style.css sheet via our index.php. Make sure that we're going to the Roux style.css sheet, and I'm going to copy blogHeader right here. The rule that's on line 59, paste it right below, and change blog to conference.
Now you may recall when we first copied in the blogHeader we didn't just copy in blo_header01, we copied in blog_header01, 02, and 03. So let's just change the 01 to 02, Save the page, and now I'm going to go to Design view. Again, let's go to view option just to make sure that follow links continuously is still selected. And now if I refresh the page, I am on my Conference page already. I have a new header that's dedicated to the Conference page. So far so good.
Now this is fine and dandy for the General conference page, but let's see what happens when we drill down into a single post. So I'll click on Roux Academy Art Conference, and it switches back to the general header. WordPress is now using a single.php page template. Let's copy that file into our child theme. Again, I'll expand the Files panel, and let's scroll up a little bit and then roll down into Custom where you'll see single.php. I'm going to copy that file, collapse custom, select roux, and paste it right in.
Okay, we'll collapse the Files panel again, and let's open up the file that we just brought in. So here's roux, I'll go to code, and as you can see, it's from the roux theme folder. Unfortunately, we can't just use a single-conference.php file like we did a header-conference.php file. We'll need to create some code to discover which category is applied for a specific post and then serve the appropriate header, and I've got just the code to do that.
Let's go to File > Open, and we'll go to our Desktop where the exercise files are, and here's Chapter 07/07_03, and there you'll see the single_header_code file, open that, and here's our PHP code block. Let me explain a little bit of what's going on with this. There is basically two different areas here. The first going from line 3 to line 7, check to see if there are posts, and if there are, it gets whatever categories that there are assigned to it.
Now this is a pretty robust routine that I developed for a client once, so I decided just to use it rather than strip it down to the bare minimum. This second part of the routine that you see here actually goes through and gets whatever parent category that there is, so it gets the top level category, and we'll use that instead of multiple categories that appear. All right, so the rest of the routine goes through and looks to see what categories are being used. The very uppermost category that's being used that becomes the top parent and then it looks to see if the top parent is conference, get_header ('conference') and otherwise get_header ('archive').
And this last bit of code here wp_reset_query is very important. Because we have looped through the posts, you want to make sure to reset the query so that any other times that you engage in the loop on the page it will work correctly. All right, so all we need to do is to copy our sample code, go to single.php, and I'm just going to replace this one get_header file that you see here. We'll save that code, and now let's head back over to index.php, and we're in the single mode already, so I'll just refresh, and there is my new header.
So it looks like we've got our header rocking to the Conference category, and if you wanted to you could apply the same techniques to the sidebar and footer. It's all up to your design.
Get unlimited access to all courses for just $25/month.Become a member
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.