Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
A poorly kept secret about pages, is that within WordPress, you can create custom page templates. While the intention of these templates, was for theme authors to be able to offer up alternative display options for page content, you can also use them to create custom landing pages and other types of experiences on your site. Custom page templates may be one of the most powerful tools available to you as a WordPress developer because you can build as many as you like and add any code you like within them.
To give you an example of how powerful these custom page templates are, let me show you a project I've been working on called Ciao Bambino. Here, if you navigate to any of the menu elements here on the main menu, you'll see different custom page templates. Here's one of them that shows a side bar on the left, then the main content in the middle, and then the side bar on the right. Here's another that shows content on the left, and here we have several custom loops that display the latest content from specific categories. And this is a final one, that again has custom side bars on the left and right, and content in the middle.
Now that you've seen what can be done, let me show you how to do it. Let's say we want to create a new custom page template that gives the site user the option of not having a side bar in the page. So instead we're going to align all the content to the middle. This is a common request for WordPress themes, and a no side bar page template should always be in a WordPress theme. And this is where you'll see why we've been separating our layout styles into a separate CSS file because now we can apply a new set of styles just for that custom page template.
But first, we have to create the custom page template itself. Back in NetBeans, the first thing I want to do do is create a new folder for my custom page templates, so if I add more of them I know where they all are. So, I'm going to create a new folder and call it page templates. And then, I'm going to grab the page.php file, copy it, go into page templates and paste it in. So now I have two files named page.php and that doesn't work. So, I'm going to go and change the name of this file to page-nosidebar and then I can open page-nosidebar.
Now, of course, this is just the hard copy of page.php. And that's not going to work. So here, I'm going to create a custom page template and it's done with five lines of code. At the very top here, I'm going to remove the existing comment and then I'm going to type in, template name, page with no sidebar. Save page no sidebar, go back to WordPress and edit the current page. And all of a sudden you'll notice here, on the right hand side, under Page Attributes, you now have a new option that's called Template.
If you drop down that option, here we have page with no sidebar, the template we've just created. I can now update my post and the post will be running the page with no sidebar template instead of the default template. But of course, nothing has changed and that's because we now need to apply new styles. So what I need to do now is go into my layouts, and here I'm going to take content sidebar and copy it and I'm going to paste it back in. And then I'll change the name here, to no sidebar and I'll open this style sheet and here I can strip out all the code I don't need, because this style sheet will only apply to the page with no sidebar.
That means I can get rid of all of my media queries down here because all the media queries relate to shifting the content left and right and the default styles are just centering everything to the middle. And it also means I can get rid of all this archive styles and everything else. So, to make it simple, I've created a code snippet that just has the styles we need. So, I'll copy out these styles and we see it's a much shorter list. I'll just highlight everything in nosidebar.css and paste in the new styles. And if you go through this, you'll notice I haven't actually changed anything, I've just removed a bunch of the styles and the media queries.
Now I'll save nosidebar.css, but of course, nosidebar.css is a new file, so we now need to enqueue it into our theme. But we need to enqueue it, so it only queues up when we're on the correct page template. So, here I'll first go into functions.php and find where I'm calling in the current layout. So here, inside the enqueue functions, you'll remember we call in first style.css and then we call in the layouts. So here we have layouts contents sidebar. So here, we need to get WordPress to identify when we are using the custom page template and then call in the alternate style sheet for the custom page template.
Back in my code snippets, I have just the function for that purpose. So here, if you copy out the last function here, and just replace the call to content sidebar with this new function, you'll see what happens. First we use a function called is page template that tests whether or not you're currently using a page template. And we're saying, if we are using the page template found under page templates slash page no sidebar, then enqueue the following style sheet. And then we're enqueueing layouts no sidebar.
Else, so in all other cases, we're enqueueing the regular content sidebar.css. So now with a new function in place, when I reload my page, that is the no sidebar page, we get a page with no sidebar and all the content aligns to the middle. If I jump to any other page, that is the regular template page, I just get the regular template. And what you've seen here, is just the tip of the iceberg. Using custom page templates, along with custom style sheets and conditional functions, you can create totally custom experiences for each individual page on your site.
And there really is no limit here. So you can do pretty much whatever you want with custom page templates.
Get unlimited access to all courses for just $25/month.Become a member
58 Video lessons · 60352 Viewers
61 Video lessons · 92896 Viewers
82 Video lessons · 104249 Viewers
56 Video lessons · 107330 Viewers
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.
Your file was successfully uploaded.