A simple way to get the page ID of the template used in WordPress _php instance

Source: Internet
Author: User
Tags php template

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!

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.