How To Add Custom Page Template In WordPress
- By : Mydatahack
- Category : php, Web Technologies
- Tags: Custom Page, Wordpress
In modern web development, we try to avoid building a website from scratch if we can. Websites are usually built on top of a platform like Sitecore, Drupal, WordPress and so on. When I first thought about creating this blog, I tried to code everything from scratch, going against the trend. After a few months of PHP coding, I realised the effort would be too much. So, I decided to use WordPress. It is probably the easiest platform to create a blog site with enough customisability.
The power of WordPress lies in rich template choice and its customisability. You build your pages by using the existing templates and functions. It’s like Lego. It provides blocks and we can combine them to build something new.
In this post, I will go through how to add a custom page template in WordPress. It’s the low-hanging fruits in WordPress customisation with high effort-reward ratio. In fact, my home page is on the custom page template. I customised the PHP code to display latest blog entries per category.
Steps
(1) Log into wp-admin.
(2) Go to Appearance > Editor
(3) Find Single Page under Theme Files
(4) Copy and paste the code and save it with a new name (e.g. custom_page.php). You can edit it as you like. Make sure to include the tag as below. You can add this at the beginning of the file. Template Name value will be displayed as the page name under theme files.
1 2 3 4 5 | <?php /* Template Name: My Home Custom Template */ ?> |
(5) Once you finish editing, upload the file in the folder for the theme you are currently using in the server (e.g. /home2/mydatahack/public_html/wp-content/themes/mytheme/).
To connect to the web server, you have to follow the host provider’s instruction. I set up SSH connection to the web server and used WinSCP to transfer files from my Windows machine. You can use any FTP client.
(6) You will see your custom template with the name you specified in Theme Files.
(7) You can create a new page with the custom template selected as Page Attribute.
You can now have the page with custom template like my home page.
Fun!