How to create a WordPress page template?
发布时间:2 September 2023浏览量:376
The steps for creating a WordPress page template are as follows:
- Create the template file using a text editor: Open your text editor (such as Notepad++, Sublime Text, etc.), create a new file, and save it as
.phpFormat. For example, it could be namedtemplate-custom.php。 - Add template header information: Upon creation
.phpAt the beginning of the file, add the following template header information: phpCopy code<?php /* Template Name: Custom Template */ ?>Please ensureTemplate NameThe value that follows is the name you wish to display in the WordPress page template selector. - Add page content: Within the template file, you may add HTML and PHP code as required to define your desired page layout and styling.
<?php get_header(); ?> <div id="primary"> <main id="main" class="site-main" role="main"> <!-- Your custom template content goes here --> </main><!-- #main --> </div><!-- #primary --> <?php get_footer(); ?>In the aforementioned code,get_header()和get_footer()This is the function used to call the header and footer templates of a WordPress theme. - Save file: Save yours
.phpDocument. - Upload template file: Will be saved
.phpUpload the file to your WordPress theme folder. Typically, the theme folder is located atwp-content/themes/your-theme/。 - Create a page in the WordPress backend and select a template: Log in to your WordPress dashboard and create or edit a page. In the “Page Attributes” box on the right-hand side, you should see a dropdown menu labelled “Template”. Select the custom template you have just created and save the page.
You have now successfully created a WordPress page template and applied it to a specific page. Within this template, you can customise the page's structure and styling as required.








