0
0
WordpressHow-ToBeginner · 3 min read

How to Create a Page Template in WordPress Easily

To create a page template in WordPress, create a new PHP file in your theme folder and add a Template Name comment at the top. Then, add your custom HTML and PHP code inside this file. You can select this template when editing a page in the WordPress admin under the Page Attributes section.
📐

Syntax

A WordPress page template starts with a PHP comment that names the template. This comment tells WordPress the file is a template you can select.

  • <?php - Opens PHP code.
  • /* Template Name: Your Template Name */ - Defines the template name shown in the admin.
  • HTML and PHP code below defines the page layout.
php
<?php
/* Template Name: Custom Page Template */
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Custom Page Template</title>
    <?php wp_head(); ?>
</head>
<body>
    <h1>This is a custom page template</h1>
    <?php wp_footer(); ?>
</body>
</html>
Output
A webpage with heading: 'This is a custom page template' when this template is applied to a page.
💻

Example

This example shows a simple custom page template that includes the WordPress header and footer, and displays a custom message in the page content area.

php
<?php
/* Template Name: Simple Custom Template */
get_header(); ?>

<div class="content-area">
    <h2>Welcome to the Simple Custom Template</h2>
    <p>This page uses a custom template to change its layout.</p>
</div>

<?php get_footer(); ?>
Output
Page displays the site header, then a heading 'Welcome to the Simple Custom Template' with a paragraph, followed by the site footer.
⚠️

Common Pitfalls

  • Forgetting the Template Name comment means WordPress won't recognize the file as a template.
  • Placing the template file outside the active theme folder will prevent it from appearing in the admin.
  • Not calling get_header() and get_footer() can cause missing site header/footer.
  • Using incorrect file permissions can make the template file unreadable.
php
<?php
// Wrong: Missing Template Name comment
// This file won't appear as a template

get_header();
echo '<h1>Missing Template Name</h1>';
get_footer();

// Right: Add Template Name comment
/* Template Name: Fixed Template */
get_header();
echo '<h1>Fixed Template</h1>';
get_footer();
📊

Quick Reference

StepDescription
Create PHP fileAdd a new .php file in your active theme folder
Add Template NameInclude a PHP comment: /* Template Name: Your Name */
Add contentWrite HTML/PHP for your page layout
Use WordPress functionsCall get_header() and get_footer() for site layout
Select templateChoose your template in WordPress page editor under Page Attributes

Key Takeaways

Always start your template file with a PHP comment naming the template.
Place the template file inside your active theme folder to make it available.
Use get_header() and get_footer() to keep site design consistent.
Select your custom template from the Page Attributes box when editing a page.
Check file permissions to ensure WordPress can read your template file.