How to create a WordPress page template?

发布时间:2 September 2023浏览量:376

The steps for creating a WordPress page template are as follows:

  1. 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 .php Format. For example, it could be named template-custom.php
  2. Add template header information: Upon creation .php At 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.
  3. 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.
  4. Save file: Save yours .php Document.
  5. Upload template file: Will be saved .php Upload the file to your WordPress theme folder. Typically, the theme folder is located atwp-content/themes/your-theme/
  6. 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.