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

Source: Internet
Author: User

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


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 a different template to display the page, such as login, registration and submission page, etc., these pages with the normal page is different, At this time WordPress provides page templates 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, the custom column name: _wp_page_template, the value of the template file name:

If the default template is page.php, then the value of _wp_page_template is: default. WordPress does not add this custom column if it is the default template from start to finish
If it is a custom page template under the theme root, 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 belongs to a hidden custom column, you cannot see this field in the custom column of the page editing page.

Get the page ID from the page template

I created a login page template, named login.php, and the background already has a page in use this template, then I can use the following function to get the page ID of this template using login.php:

function Get_page_id_from_template ($template) {  global $wpdb;  Multiple pages using the same template I can't do 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, what is the page ID for? Is it possible to get a link to the login page by ID:

> Login

Some people will also ask, through the page title and alias can not also get the page ID? If the subject is for the customer, do you know what title the customer will use? Force the customer to use the title you specified? Then you are too impersonal!

Articles you may be interested in:

    • Introduction of template file for WordPress theme making
    • WordPress Theme Authoring in Get header template and bottom template
    • Explain the PHP functions used to get Comment templates and search forms in WordPress

http://www.bkjia.com/PHPjc/1087275.html www.bkjia.com true http://www.bkjia.com/PHPjc/1087275.html techarticle A simple way to get the page ID of the template used in WordPress, Wordpressid What is a template? By default WordPress is using the theme directory under the page.php to display the page as a template ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.