Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In the previous movie we created a custom page template and remove the title from that template. However, the method I showed was a rather crude one. I copied a bunch of code, this code here, from a different template file, the contents-page.php file and pasted it directly into my custom page template. This is a quick and dirty way of doing things, but it's not necessarily the most effective way of doing it. Now I am going to show you a much cleaner and just as quick way of doing the same thing.
Rather than copying and pasting the code, I am going to use one of the many conditional tags built into WordPress, to test to see if were using a custom page template and then display the content accordingly. Conditional tags are simple if then statements that can help you consolidate a lot of code into a single template file, and that have the computer figure out what to display, and where to display it, and when to display it. The first thing I am going to do is reset my and no-title-page custom template file, back to the way it was originally.
I am going to delete all this code that I pasted and then I am going to find the original file I copied; that would be that page template file from the twentytwelve theme. And I will find the original call, here it is get_templete_part content page. Like I explained before, what happens here is WordPress says, hey there's a file that's called content-page and the contents from that file should go right here.
So when I save this custom page template and reload my page that's using it, you'll see the title is now back and all the content is too. The next thing I am going to do now is tell the file is being that's being called the content-page file, to display content differently, if we're using this page template. I am going to do that using conditional tags. Conditional tags are small functions built into WordPress that allow us to test for different scenarios, and then do things based on those tests. Basically you are saying things like if this is the homepage then do the following, if it's not, do something else.
In this case, we want to test for whether or not a certain page template is going to be used. We are going to test whether this is a page template and there's a conditional function just for that. It's called is_page_template and it's used the following way. You ask is_page_template, and then you define what page template you're looking for. So we are going to use this exact function here inside the template file. The template file in question is this one, content-page, and since I am going to make a change to it, I first need to copy it into my child theme.
So I'll copy it into child theme and then open it in my code editor. And then I need to find the content I want to change. It's this right here, the Now I am going to wrap this in a new function that uses a conditional statement. So I will start with php delimiter, then I will say if is_page_template and put in the name of the file I am looking for, no-title-page.php. I'll end my parentheses and I am putting curly bracket and then anything that's within the curly bracket will then be run if this is_page_template, and my PHP delimiter, so that HTML can run underneath it, and then I'll put the php delimiter back in and end my curly bracket.
So what happens now is when content page runs, it will go, hey, is this page template called no-title-page.php? If it is, then display the title. Which of course is exactly opposite of what we want, but I am just going to test to see that this works first, so I'll save content page, go back to my sites and reload the page that has the custom page template on it. As you can see we can still see the title. And if I go to a different page, you'll see that this page does not have title. It should say about here at the top but it doesn't.
So now I want to reverse it. I'll go back to my code and then I'll say exclamation point is_page_template. The exclamation point means not. So now we are saying, if this is not this page template then display this content. Otherwise, meaning it is the page template, do not display this content. I'll save content page again, reload the page that should have a title on it. Now you see the title right here, About, and then I can reload the page that should have a title and you see Welcomes disappears. So no title.
Using conditional tags, or even regular conditional if then statements in your child theme, you can turn on and off elements and functions at will to create highly customized behavior. I use conditional statements constantly in my sites, because they make the themes lean and clean, and make it easier to manage the template files. The rule of thumb here is this, if you want to show or hide something on a page or post and you're planning on making a new template, see if you can get the computer to test for a condition instead so you don't have to write out the entire new template.
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.
Your file was successfully uploaded.