If you visit a wordpress page such as http://myprogrammingnotes.com/sample-page, we know that after wordpress gets the content of the page from the database table wp-posts, it will load a page template to display the page, which is done in template-loader.php. How does wordpress get the current page template name? WordPress gets the page template name in the following order.
- Check if there is a meta _wp_page_template in the wp_post_meta table that is related to the page. If there is, use the meta value as the page template.
- Check if there exists a file called page-sample-page.php in current theme directory.
- Check if there exists a file called page-2.php in current theme directory, where 2 is the post id of the page.
- Check if there exists a file called page.php in current theme directory.
From above, we know that WordPress can get the name of the page template by the id of the page, or by the name of the page. If we need different page templates for different pages, we should create page templates named after page-id.php or page-name.php. If we use a general template for all pages, we should put a file named after page.php under the theme directory. Also note that there is no default page template, so we should create at least one page template for our theme.