Project Zero

Custom Post / Page Templates

By default, WordPress will look for page.php when dealing with static pages or custom hierarchical post types, or single.php when dealing with blog posts or custom non-hierarchical post types. You can use page-$id.php or page-$slug.php if you want to assign a specific template to certain pages, or you can create a custom file, add a header comment, and create a Custom Page Template.

To create a custom page template, make sure you add the following code to your header.

<?php 
/**
 * Template Name: Custom Page Template
 * Template Post Type: page
 */
?>

You can save the file by any name, as long as it doesn’t interfere with the typical WordPress template structure. To apply a template to a page:

If you are using the Block Editor (Gutenberg)

  1. Go to the sidebar in your page editor and make sure you have selected Page.
  2. Go to Template, and select the template that has been applied (usually “Default template”).
  3. In the drop down menu that appears, select your required template.

If you are using the Classic Editor (or ClassicPress)

  1. Find out.

You can also create a template for different post types, including blog posts. To create a template for a blog post, add the following code to the top of your file:

<?php 
/**
 * Template Name: Custom Post Template
 * Template Post Type: post
 */
?>

You can assign the template to multiple post types if you wish:

<?php 
/**
 * Template Name: Pages, Projects, and Products (Oh my!)
 * Template Post Type: page, project, product
 */
?>

In Project Zero, custom page and post templates are named template-{unique_slug}.php, so you can differentiate it from other files. Custom page templates are named template-page_{slug}.php and custom post templates are named template-post_{slug}.php. While you can use any file name you wish, it may help to keep them in a system so you can find them and edit them later.

Join the conversation

Your email address will not be published.


This site uses Akismet to reduce spam. Learn how your comment data is processed.