Home > Backend Development > PHP Tutorial > An easy way to get the page ID of the template used in WordPress

An easy way to get the page ID of the template used in WordPress

WBOY
Release: 2016-07-29 09:08:15
Original
1133 people have browsed it

What is a template? By default, WordPress uses page.php in the theme directory as a template to display pages, but sometimes we need different templates to display pages, such as login, registration and submission pages, etc. These pages are different from ordinary pages. At this time, WordPress provides page templates so that developers can customize the appearance and even functions of WordPress pages.
Matching of page template php files

WordPress records the template used by the page through a custom column. The custom column name: _wp_page_template, the value is the file name of the template:

If it is the default template page.php, then The value of _wp_page_template is: default. If it is the default template from beginning to end, WordPress will not add this custom column
If it is a custom page template in the theme 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, it is a hidden custom column, so you This field cannot be seen in the custom column on the page edit page.

Get the page ID through the page template

I created a new login page template, named login.php, and there is already a page using this template in the background, then I can use the following function to get the login.php Template page id:

function get_page_id_from_template($template) {
  global $wpdb;

  // 多个页面使用同一个模板我就没辙了
  $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;
}

Copy after login

Many people may ask, what are you doing to get the page id? Isn’t it possible to get the link to the login page through the id:

<a href="<&#63;php 
echo get_permalink(get_page_id_from_template('login.php'))
&#63;>>登录</a>
Copy after login

Some people may also ask, can’t the page id also be obtained through the page title and alias? If the theme is for a client, do you know what kind of title the client will use? Force customers to use the title you specify? Then you are so inhumane!

The above introduces a simple method to obtain the page ID of the template used in WordPress, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template