Get the App
SLTechnology News&Howtos  ›  Development  › 

How to get the page ID of the template used in WordPress

Shulou Source: shulou.com Published: 2022-06-03 13:35:19 09月22日 Update

This article mainly introduces "how to get the page ID of the template used in WordPress". In daily operation, I believe many people have doubts about how to get the page ID of the template used in WordPress. Xiaobian consulted all kinds of materials and sorted out simple and easy to use operation methods. I hope to answer the doubts of "how to get the page ID of the template used in WordPress"! Next, please follow the small series to learn together!

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 contribution pages, etc. These pages are different from ordinary pages. WordPress provides page templates that allow developers to customize the appearance and even functionality of WordPress pages.

Page template php file matching

WordPress is a 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 value of_wp_page_template is: default. WordPress will 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 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 belongs to a hidden custom column, so you can't see this field in the custom column of the page edit page.

Get page ID from page template

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

function get_page_id_from_template($template) { global $wpdb; //multiple pages using the same template I can't do anything $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;} At this point, the study of" how to get the page ID of the template used in WordPress "is over, hoping to solve everyone's doubts. Theory and practice can better match to help you learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

Tags: Pages templates columns learning topics files names file names more help login different practical ordinary next underline from beginning to end functions functions can make Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Apple Huawei MariaDB MySQL