What is a template? By default, WordPress uses the page under the topic directory. php is used to display pages as templates, but sometimes we need different templates to display pages, such as logon, registration, and contribution pages. These pages are different from common pages, at this time, WordPress provides a page template that allows developers to customize the appearance and functions of WordPress pages.
For more information about how to use a page template, see what is a WordPress page template.
Page template php file matching
In WordPress, the template used by the page is recorded through a custom column. The custom column name is _ wp_page_template, and the value is the template File name:
If the default template is page. php, the value of _ wp_page_template is default. If the default template is used from the beginning, WordPress does not add this custom topic.
If it is a custom page template under the topic root directory, then the value of _ wp_page_template is the file name, such as: page-login.php
If it is a page template under the theme subdirectory, then the value of _ wp_page_template contains the path, such as: templates/page-login.php
Because the name of this custom column starts with an underscore and is a hidden custom column, you cannot see this field in the custom column on the page editing page.
Obtain the page ID using the page template
I created a new login page template named login. php, and there are pages in the background using this template, then I can use the following function to obtain the use of login. page id of the php template (thanks to Sola's suggestions ):
The code is as follows: |
Copy code |
Function get_page_id_from_template ($ template ){ Global $ wpdb;
// I can use the same template for multiple pages. $ 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 may ask, what do I do when I obtain the page id? Can I use the id to obtain the link to the logon page:
The code is as follows: |
Copy code |
<A href = "<? Php Echo get_permalink (get_page_id_from_template ('login. Php ')) ?> Logon </a> |
Someone may ask, can I obtain the page id through the page title and alias? If the topic is used by the customer, do you know what kind of title the customer will use? Force the customer to use the title you specified? Then you are too unfriendly!