WordPressCustom templates are becoming the key factor for any content management system out there. Possibility to create custom front end lets many developers to offer their own custom templates, be it game website templates, eCommerce website templates or others. But have you ever had the need to create a custom template for a specific page on your WordPress-controlled website? Maybe you have created a page on your site that needs to include a customized sidebar, or maybe you want to create a custom archive page for specific categories of blog posts? Whatever the reason, it’s something I am constantly doing within my WordPress websites.

So, how is it done? Well, there are actually quite a few different ways to develop a custom page template for WordPress.

Simple Customizations

First, you could always hardcode specific information in your main template files. For instance, if you just want to do something simple like excluding the sidebar from one or two pages on your website, you could add that information right into your index.php (or your sidebar.php file or which ever file makes the most sense). That might look something like:

<?php if( !in_array( $post->ID, array(1,7,43) ) ) { get_sidebar(); } ?>

In the code above, if the page/post has an ID of “1”, “7” or “43”, the sidebar will not be included.

Utilizing the WordPress Template Hierarchy

The WordPress template hierarchy can also be extremely useful in trying to develop a custom page template.

Include Custom Template Files

Although this particular tip doesn’t specifically utilize the template hierarchy, it does sort of play off of that concept, so I wanted to include it here. If you want to include a custom file on a specific page or pages, you can always utilize some of the template inclusion methods WordPress offers. For instance, building off of the example above, let’s say you want to include a custom sidebar on the pages/posts with IDs of “1”, “7” and “43” rather than just excluding the sidebar altogether. That code might look something like the following.

<?php if( in_array( $post->ID, array(1,7,43) ) ) { get_sidebar('custom'); } else { get_sidebar(); } ?>

In the code above, we are once again checking to see if the current page/post ID is 1, 7 or 43. This time, however, if the ID is one of those, we are including a different sidebar. In this case, we are including the “custom” sidebar, which will pull in a PHP file called “sidebar-custom.php” in your theme’s folder (assuming that file exists). If the ID is not one of those, we are simply including the default sidebar (“sidebar.php”).

There are a few WordPress methods of which you need to be aware, and all of them function in a similar manner. They are:

With the first three methods, you only supply a single parameter. With the last method, you supply two parameters.

You only supply one parameter for the first three methods because the first part of the filename is implied by the function you are calling. For instance, if you call “get_header()”, WordPress is automatically going to limit the results to only files that start with “header.” The parameter indicates which “header” file you want to include. So, if you use a keyword like “foo” in the get_header() function, WordPress will search for “header-foo.php”.

The last method listed above, however, accepts two parameters. The first parameter is the template part you want to include, which equates to the first part of the filename. The second parameter indicates the specific instance of that template part. Therefore, if you want to include a file called something like “loop-archives.php”, you would use a call like get_template_part('loop','archives').

Template Files for Specific Pages

You can also simply create template files for specific pages. To do so, you would create a new PHP file and give it a filename that starts with “page-“, followed by the slug or ID of the page to which you want it applied. For instance, if you want to apply a custom template to the page with an ID of 43, you would create a file called “page-43.php”. Alternatively, if you wanted to apply a specific template to a page with the slug of ‘this-is-a-test-page’, you would create a file called “page-this-is-a-test.php”.

This method is really useful if you are only applying the template to a single page, but it can start to get tedious if you want to apply the template to multiple pages. It can also be frustrating if the slug and/or ID ever changes. Finally, the specific method I’ve shown above only works for WordPress “pages” (not “posts” or “archives” or anything else). To utilize this method for a different type of WordPress item, you would need to consult the codex to find the appropriate file-naming scheme for that particular item.

Custom Page Templates

One more option is to build a full-fledged custom page template. This concept can seem a little overwhelming at first, but it’s actually very simple. To start, you simply need to create a PHP file and add the following code to the top of it:

/* Template Name: [Your Page Template Name] */

Obviously, you should replace “[Your Page Template Name]” with whatever you want the real name of your template to be. Then, start building your template from there. You can utilize all of the normal WordPress template functions within your custom page template. One thing of which you should be aware, though, is that the custom page template will automatically include the “functions.php” file from your main theme, so be careful not to try to redefine custom functions and be aware that any pre-existing custom functions will have an effect on the output of your custom template.

Once you’ve created the custom template and uploaded it to your theme’s folder, you then have the option to apply that template to any new pages you create in WordPress (or you can edit existing pages to apply that template retroactively). When editing/adding the page, you’ll see a dropdown menu (HTML select element) for “Template”. If you don’t have any custom template files in your theme, that dropdown menu will be empty. Once you create a custom template, though, you’ll then see that option available inside the dropdown menu.

Wrap-Up

Obviously there are still other ways to accomplish this, too. Your particular situation is most likely going to dictate which of these methods (or any other available methods) you end up using, and more than likely you’ll end up using some combination of multiple methods when actually using custom templates for specific pages on your WordPress website, but I hope this information helps you make an informed decision.