How to create template in WordPress Part 2

Posted on

Well, WordPress makes that incredibly easy to do we simply create a new file in the theme folder and name it page – and then we can include either the slug name of the page that we’re targeting or the ID number of the page so we could either include 14 or we could spell out the slug and say portfolio that’s it now if we copy and paste code from page PHP into this newly created file we can customize this any way we would like so, for example, what if on the portfolio page we do not want to display the title just delete that line refresh you can see that it’s gone but if we go to any other page the title is still present well that wasn’t the most exciting change let’s give us something a bit more interesting what if we wanted the title to sit on the left side and we wanted this content to sit on the right side so sort of a two-column layout.

you can do whatever you would like so we could say a column container give it a class of clear fix says title column text column so we’ll move the content into this text column will output the title and title column so we’ll say H to use the PHP excuse me the WordPress function name the title so you can see that it’s back in but now we have this unique HTML structure to play with so we can just add a bit of CSS and we’ll have a two-column layout so let’s head over to our style sheet and add a comment to stay organized to column title layout so the title column width 30% float it to the left the text column take up the rest of the width and we’ll float it to the right so if we refresh you can see that. we have a two-column layout now I’m not going to claim that I love the way this looks visually but we set it up very quickly and it’s just to illustrate the point that you can very easily create different layouts for different pages ice creating a new file on your theme folder named page – and then either the slug or the ID.

So now we’ve learned how to assign very specific theme files to very specific pages based on their name or ID however what if we wanted to create a template that could be used by multiple pages so for example you can see we have links down here in the footer frequently asked question Terms & Conditions privacy policy what if we wanted just these three pages to all share a common layout but none of the other pages WordPress makes it very simple so we’llhead over to the code we’ll create a newfile you can name it anything we would like I will call it special template now let’s copy and paste the code from our standard page PHP into special template and we just want to include a comment up near the top of the file that says template name special layout or you can name it anything you would like and then close the comment this comment is essentially registering the template with WordPress so WordPress is aware that it exists so for example if we head over to the dashboard click on pages and find one of the pages that is linked to from the footer privacy policy war we can see that in the bottom right cornerunder page attributes there’s a template field and we can click it and select special layout the file we just creatednow obviously if we click update and refresh nothing will change because remember we literally just copied and pasted the code from page standard page PHP so let’s go ahead and make some quick customizations.

Leave a Reply

Your email address will not be published. Required fields are marked *