Home >Backend Development >PHP Tutorial >A simple way to get the page ID of the template used in WordPress, wordpressid_PHP tutorial

A simple way to get the page ID of the template used in WordPress, wordpressid_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 09:01:501054browse

A simple way to get the page ID of the template used in WordPress, wordpressid

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 custom columns. The custom column name is: _wp_page_template, and 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 the default template is used 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 and is a hidden custom column, you cannot see this field in the custom column on the page editing page.

Get page ID through 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 page ID using the login.php template:

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;
}

Many people may ask, what is the purpose of getting the page ID? Isn’t it possible to get the link to the login page by id:

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

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

Articles you may be interested in:

  • How to introduce template files in WordPress theme production
  • Get the header template and bottom template in WordPress theme writing
  • Explain the PHP functions used to obtain comment templates and search forms in WordPress

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1087275.htmlTechArticleA simple way to get the page ID of the template used in WordPress, wordpressid What is a template? By default, WordPress uses page.php in the theme directory as a template to display the page...
Statement:
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