search
HomeBackend DevelopmentPHP TutorialAn easy way to get the page ID of the template used in WordPress

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

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="<?php%20%0Aecho%20get_permalink(get_page_id_from_template('login.php'))%0A?>>%E7%99%BB%E5%BD%95</a>%0A

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

%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20

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

%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20

%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20

">
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
wordpress后台乱码怎么办wordpress后台乱码怎么办Feb 03, 2023 pm 01:48 PM

wordpress后台乱码的解决办法:1、在wordpress的“wp-admin”文件夹下找到“admin.header.php”文件;2、将“charset”属性值设置为“UTF-8”格式即可恢复正常。

如何解决wordpress标签错误问题如何解决wordpress标签错误问题Feb 03, 2023 pm 02:03 PM

wordpress标签错误的解决办法:1、找到并打开wordpress的“wp-includes”目录下的“class-wp.php”文件;2、修改内容为“$pathinfo = isset( $_SERVER['PATH_INFO'] )?mb_convert_encoding($_SERVER['PATH_INFO'],'utf-8','GBK') : '';”即可。

如何解决 golang 中的 "undefined: template.Must" 错误?如何解决 golang 中的 "undefined: template.Must" 错误?Jun 24, 2023 pm 09:00 PM

Go语言是一种越来越受欢迎的编程语言,它的语法简洁,性能高效,易于开发。Go语言中提供了强大的模板引擎——"text/template",但是在使用时,有些人可能会遇到"undefined:template.Must"的错误,下面是解决该错误的方法。导入正确的包在使用"text/template"模板引擎时,需要导入"text/templat

Golang和Template包:创建个性化的用户界面Golang和Template包:创建个性化的用户界面Jul 18, 2023 am 10:27 AM

Golang和Template包:创建个性化的用户界面在现代的软件开发中,用户界面往往是用户与软件进行互动的最直接的途径。为了提供一个好用、美观的用户界面,开发者需要灵活的工具来创建和定制用户界面。而在Golang中,开发者可以使用Template包来实现这一目标。本文将介绍Golang和Template包的基本用法,并通过代码示例展示如何创建个性化的用户界

Vue项目中如何实现数据的分页和显示优化Vue项目中如何实现数据的分页和显示优化Oct 15, 2023 am 09:27 AM

Vue项目中实现数据的分页和显示优化在Vue项目中,当页面需要展示大量数据时,通常需要进行数据的分页和显示优化以提高用户体验,本文将介绍如何使用Vue实现数据的分页和显示优化,并提供具体的代码示例。一、数据分页数据分页是指将大量数据按照一定的规则分割成多页,并在页面上进行分页显示。Vue项目中可以使用如下步骤来实现数据分页:定义数据源首先,定义一个包含所有数

通过Golang的Template包实现数据可视化通过Golang的Template包实现数据可视化Jul 17, 2023 am 09:01 AM

通过Golang的Template包实现数据可视化随着大数据时代的到来,数据可视化成为了信息处理和分析的重要手段之一。数据可视化能够以简洁直观的方式展现数据,帮助人们更好地理解和分析数据。在Golang中,我们可以使用Template包来实现数据可视化功能。本文将介绍如何利用Golang的Template包实现数据可视化,并提供代码示例。Golang的Tem

2023年最新WordPress视频教程推荐2023年最新WordPress视频教程推荐Oct 25, 2019 pm 01:12 PM

本次PHP中文网整合了相关的视频教程,中文手册,以及相关的精选文章安利给大家,统统免费!!!通过我们分享的视频,可随时随地免费观看教程视频,也不需要迅雷或者百度网盘下载了。

wordpress是哪一年的wordpress是哪一年的Feb 01, 2023 am 10:26 AM

wordpress是2003年发布的;Matt于2003年5月27日宣布推出第一版WordPress,受到了社区的欢迎,它基于b2 Cafelog并有显著改进;WordPress的第一个版本包括全新的管理界面、模板、XHTML 1.1兼容模板、内容编辑器。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software