As mentioned in the Template Hierarchy page, you can create a template for a specific page. To create a template for one specific page, copy your existing page.php file and rename it with your page’s slug or ID:
- page-{slug}.php
- page-{ID}.php
For example: Your About page has a slug of ‘about’ and an ID of 6. If your active theme’s folder has a file named page-about.php or page-6.php, then WordPress will automatically find and use that file to render the About page.
To be used, specialized page templates must be in your theme’s folder (i.e. /wp-content/themes/my-theme-name/ ).
Using Conditional Tags in Page Templates
You can make smaller, page-specific changes with Conditional Tags in your theme’s page.php file. For instance, the below example code loads the file header-home.php for your front page, but loads another file (header-about.php) for your About page, and then applies the default header.php for all other pages.
1 2 3 4 5 6 7 |
if ( is_front_page() ) : get_header( ‘home’ ); elseif ( is_page( ‘About’ ) ) : get_header( ‘about’ ); else: get_header(); endif; |
You can learn more about Conditional Tags here.
Identifying a Page Template
If your template uses the body_class() function, WordPress will print classes in the body tag for the post type class name (page), the page’s ID (page-id-{ID}), and the page template used. For the default page.php, the class name generated is page-template-default:
1 |
<body class=“page page-id-6 page-template-default”> |
Note:
A specialized template (page-{slug}.php or page-{ID}.php) also gets the page-template-default class rather than its own body class.
When using a custom page template, the class page-template will print, along with a class naming the specific template. For example, if your custom page template file is named as follows:
1 |
<?php /* Template Name: My Custom Page */ ?> |
Then then rendered HTML generated will be as follows:
1 |
<body class=“page page-id-6 page-template page-template-my-custom-page-php”> |
Notice the page-template-my-custom-page-php class that is applied to the body tag.