Adding a custom page template
WordPress page templates can be used to have a different look and feel on different pages. By default Chap theme comes with 5 page templates:
These templates can be applied by selecting on from the Page Attributes metabox while editing the page.
Adding a custom template
Custom page templates should be placed in the child theme’s root directory. A template that should show up in the Page Attributes metabox should be named with the
template- prefix, such as
The template should be based on the theme’s
page.php or any of the default
The comment block on the top of the file specifies the display name that is used for the template in the Page Attributes metabox.
Since the Chap theme uses a template wrapper that eliminates the need to duplicate layout code, such as header, footer or sidebars, the page template file is very slim, containing only calls to render the page header and content.
while loop is not necessarily required as the page will generally only have 1 content “post” but adhering to WordPress standards should make the implementation compatible with plugins that potentially inject more posts to the page loop.
templates/page-header.php template that is being requested in the code is an optional template that does not exist by default, so it can be omitted as well. Simplifying things further, a custom template can look as small as this:
Here you have a template that has a display name, cannot be navigated to directly and renders the page content by calling the default
templates/content-page.php template. For reference, the
content-page.php template is what does most of the heavy lifting for putting the page content together:
Should you wish to customize that you can omit calling the
content-page.php template in your custom template file and simply add similar code instead.
Modifying the page layout of your template
Given that a custom template in Chap theme can be as slim as a couple of lines of code, how would one go about customizing how the whole page is laid out? For that filters in your child theme’s functions.php can be used.
For rendering the page layout, Chap theme uses a function to determine how many sidebars to display and which container, if any, to use. This function applies a filter called
chap_page_type that you can utilize to give your custom input based on which template is being used.
The above filter makes it so that when the custom template (
template-custom.php) is used then the page will not render any sidebars.
You can add more conditions and return different values to control multiple templates with the single filter. For example, when returning
5 you will have full control in the
template-custom.php of what is displayed below the theme header and above the theme footer.
You could split the page into 2 columns, displaying the content on the left, and something else on the right:
Or you could limit the page content to a custom width:
Or even wrap the whole content up in a segment:
With some additional filters it’s even possible to create a blank custom template that renders only the post content.