Adding new page template on wordpress theme
Adding a new page template to a WordPress theme involves creating a new PHP file in the theme's directory and registering it with WordPress. Here's a step-by-step guide:
Step 1: Create a new PHP file
In your theme's directory (e.g., wp-content/themes/your-theme
), create a new file with a name that follows the WordPress template hierarchy. For example, you can name it page-new-template.php
.
Step 2: Define the template
In the new PHP file, define the template by using the get_header()
and get_footer()
functions to include the theme's header and footer, respectively. You can also add your own HTML structure and content.
Here's an example:
<?php
/*
Template Name: New Page Template
*/
get_header();?>
<!-- Your page content here -->
<h1>Welcome to the new page template!</h1>
<?php get_footer();?>
Step 3: Register the template
In your theme's functions.php
file, add the following code to register the new template:
function your_theme_page_templates() {
register_template_directory('page-new-template.php');
}
add_action('init', 'your_theme_page_templates');
Replace your_theme
with the actual name of your theme.
Step 4: Create a new page
Go to the WordPress admin dashboard, navigate to Pages > Add New, and create a new page. In the Page Attributes meta box, select the "New Page Template" from the Template dropdown menu.
Step 5: Customize the template
You can customize the new template by adding your own PHP code, HTML, and CSS. You can also use WordPress's built-in functions and hooks to add dynamic content and functionality.
Tips and Variations
- To create a template for a specific page type (e.g., blog page), use the
page-{slug}.php
naming convention, where{slug}
is the slug of the page type (e.g.,blog
). - To create a template for a specific page ID, use the
page-{id}.php
naming convention, where{id}
is the ID of the page (e.g.,123
). - You can also create a template for a specific page hierarchy by using the
page-{parent-slug}-{slug}.php
naming convention, where{parent-slug}
is the slug of the parent page and{slug}
is the slug of the child page (e.g.,blog-page
). - To create a template for a specific post type (e.g., blog post), use the
post-{post-type}.php
naming convention, where{post-type}
is the post type (e.g.,post
).
By following these steps, you can create a new page template in your WordPress theme and use it to create custom page layouts and designs.