Hello everyone welcomes to another WordPress tutorial in an earlier lesson. we learned how to use page PHP to control the output of our WordPress pages.well in this lesson we’re going to take things one step further so what if we want to use different layouts for different pages, now most of the time all of your pages will look fairly similar and the only thing that will be different is the actual content in the main body part of the page, but there will be times when you do want to use different layouts or different output schemes or different HTML so in this video, we’re going to learn how to use conditional logic and page templates to do just that so let’s get started.
let’s give ourselves a hypothetical task let’s say that when we’re on the portfolio page. we want to bet a custom text to display after this tagline so right about here and we don’t want that text to display on any other pages only on the portfolio page now perhaps. this isn’t the most intelligent design choice or maybe this doesn’t make sense in a real-world scenario. but I think it’ll be a good proof of concept so the idea is that we already have a header file. That is used on all pages so how can we make a change in this one header.php file that only shows on one actual page. we’re going to use something called conditional logic.
The code will look like this will drop into PHP and we’re going to use an if statement so we’re saying if a condition is met and we want to do something so now anything. we place between this line and this line will run only if a condition is met so here in between these brackets is where we define what condition we’re checking for so we’ll say if we’re currently on a certain page now how can we signify the portfolio page with code. it’s quite simple if we’re viewing the portfolio page we can see that in the URL bar it ends with ID equals 14 so it’s really as simple as adding a 14 and then this is page function will take care of the rest so now any code we include here excuse me not necessarily code just text so we can say thank you for or viewing our work actually.
let’s move this to sit within this heading level 5 element and if we refresh we can see that this extra bit of text displays but if we’re on any other page. it doesn’t a quick note if your particular WordPress website does not show page ID at the end of URLs that’s okay in fact it’s desirable so let me show you an alternative way to figure out the ID simply go into your WordPress dashboard click on pages click on whatever page you’re looking for or trying to figure out the idea of and then once you click on it then in the admin the URL will say post equals and a number now if you don’t like using ID numbers at all you don’t have to you can also use slug names in your conditional logic in our code so let me go into settings and adjust my permalinks so that our URLs actually use names so I’ll change it to month and name so now if we go back to our website and we click on portfoli again you can see that the URL does not have a page ID it’s simply portfolio so that means portfolio is the slug the slug name for this page so an alternative way of adjusting or using our conditional logic would be to get rid of the 14 the ID include quotes and say portfolio so if we refresh we can see that the exact same behavior is still in place now I’ll be the first to admit that this exact example isn’t very exciting or terribly useful to include extra text in the header but it’s a proof of concept the point is that you now have conditional logic in your toolbox now anywhere in any of your theme files you can use conditional logic to display different menus or different content or different anything on any page so that closes the first chapter for this lesson conditional logic now let’s move on to something different what if we actually wanted a different template file to power this portfolio page so currently page PHP this is powering all pages but what if we had changes that we only wanted to show on this page and they weren’t subtle enough to just use conditional logic what if we actually wanted an entirely different template.