How to create new page template in wordpress
Creating a new page template in WordPress is a straightforward process that involves creating a new PHP file in your theme's directory and adding some basic code to it. Here's a step-by-step guide to help you get started:
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 starts with page-
followed by a descriptive name (e.g., page-about.php
). This file will serve as your new page template.
Step 2: Add the necessary code
In the new PHP file, add the following code:
<?php
/*
Template Name: About Page
*/
?>
The Template Name
directive is used to specify the name of your template, which will be displayed in the WordPress admin area.
Step 3: Define the page structure
In the same file, add the HTML structure for your page. This can include elements like headings, paragraphs, images, and more. For example:
<?php get_header();?>
<h1>About Us</h1>
<p>Welcome to our about page!</p>
<?php get_footer();?>
In this example, we're using the get_header()
and get_footer()
functions to include the header and footer of your theme, respectively. You can customize the HTML structure to fit your needs.
Step 4: Add any custom functionality
If you want to add any custom functionality to your page template, you can do so by using WordPress's built-in functions or by creating your own custom functions. For example, you might want to add a custom sidebar or a specific layout.
Step 5: Save and upload the file
Save the new PHP file and upload it to your theme's directory.
Step 6: Activate the new template
In the WordPress admin area, go to Appearance > Page Templates and you should see your new template listed. You can now assign this template to a page by going to Pages > All Pages, selecting a page, and choosing your new template from the Page Attributes meta box.
That's it! Your new page template is now ready to use.
Tips and Variations
- You can create multiple page templates with different names and structures.
- You can use WordPress's built-in functions, such as
the_title()
andthe_content()
, to display dynamic content on your page. - You can also use page templates to create custom post types or custom layouts for specific pages.
- If you want to create a page template that is specific to a particular post type (e.g., a page template for a specific custom post type), you can use the
post_type
parameter in your template file's name (e.g.,page-about-post-type.php
).