What is a template? By default WordPress is using the theme directory page.php to display the page as a template, but sometimes we need different templates to display pages, such as login, registration and submission page, and so on, these pages and ordinary pages are different, At this time WordPress provides a page template so that developers can customize the appearance of WordPress page or even function.
page template php file matching
WordPress is through the custom column to record the template used by the page, custom column name: _wp_page_template, the value of the template file name:
If the default template is page.php, then the _wp_page_template value is: Default. WordPress does not add this custom column if it is the default template from beginning to end
If this is a custom page template under the root of the topic, then the _wp_page_template value is the file name, such as: page-login.php
If the page template is under the theme subdirectory, then the _wp_page_template value contains the path, such as: templates/page-login.php
Because the name of this custom column begins with an underscore, it belongs to a hidden custom column, so you can't see the field in the custom column of the page edit page.
Get page ID from page template
I created a template for the login page, named login.php, and there are already pages in the background using the template, so I can use the following function to get the page ID using the login.php template:
function Get_page_id_from_template ($template) {
global $wpdb;
Multiple pages using the same template I'm out of it.
$page _id = $wpdb->get_var ($wpdb->prepare ("select" post_id ' from
' $wpdb-> Postmeta ', ' $wpdb->posts '
where ' post_id ' = ' id ' and
' post_status ' = ' publish ' and
' meta_key ' = ' _wp_page _template '
and ' meta_value ' =%s
LIMIT 1; ", $template));
return $page _id;
}
Many people may ask, get the page ID to do? Get the link to the login page by ID no:
<a href= "<?php
Echo get_permalink (get_page_id_from_template (' login.php '))
?>> login </a>
Some people will also ask, through the page title and alias can not also get to the page ID? If the theme is for the customer, do you know what kind of title the customer will use? Force the customer to use the title you specify? Then you are too impersonal!