华师 發表於 2023-6-17 00:00:00

WordPress中获取所使用的模板的页面ID的简单方法

<p>
什么是模板呢?默认情况 WordPress是使用主题目录下page.php来作为模板显示页面的,但是有时候我们需要不同的模板来显示页面,比如登录、注册和投稿页面等,这些页面跟普通的页面是有所区别,这时候WordPress提供了页面模板让开发者可以自定义WordPress页面外观甚至功能。<br><strong>页面模板php文件的匹配</strong></p>
<p>
WordPress中是通过自定义栏目来记录页面所使用的模板的,自定义栏目名称:_wp_page_template,值为模板的文件名:</p>
<p>
如果是默认模板page.php,那么 _wp_page_template 的值为:default。如果从始至终都是默认模板,WordPress不会添加此自定义栏目<br>
如果是主题根目录下的自定义页面模板,那么那么 _wp_page_template 的值为文件名,如:page-login.php<br>
如果是主题子目录下的页面模板,那么 _wp_page_template 的值包含路径,如:templates/page-login.php<br>
因为这个自定义栏目的名称是以下划线开头的,属于隐藏的自定义栏目,所以你在页面编辑页的自定义栏目中是看不到这个字段的。</p>
<p>
<strong>通过页面模板获取页面ID</strong></p>
<p>
我新建了一个登录页面的模板,命名为login.php,并且后台已经有页面在使用这个模板了,那我就可以使用以下函数来获取使用login.php这个模板的页面id:</p>
<div>
<div>
<div id="highlighter_50150">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
<div>
2</div>
<div>
3</div>
<div>
4</div>
<div>
5</div>
<div>
6</div>
<div>
7</div>
<div>
8</div>
<div>
9</div>
<div>
10</div>
<div>
11</div>
<div>
12</div>
<div>
13</div>
<div>
14</div>
</td>
<td>
<div>
<div>
<code>function</code> <code>get_page_id_from_template(</code><code>$template</code><code>) {</code>
</div>
<div>
<code>  </code><code>global</code> <code>$wpdb</code><code>;</code>
</div>
<div>
 </div>
<div>
<code>  </code><code>// 多个页面使用同一个模板我就没辙了</code>
</div>
<div>
<code>  </code><code>$page_id</code> <code>= </code><code>$wpdb</code><code>-&gt;get_var(</code><code>$wpdb</code><code>-&gt;prepare("SELECT `post_id` </code>
</div>
<div>
<code>               </code><code>FROM `</code><code>$wpdb</code><code>-&gt;postmeta`, `</code><code>$wpdb</code><code>-&gt;posts`</code>
</div>
<div>
<code>               </code><code>WHERE `post_id` = `ID`</code>
</div>
<div>
<code>                  </code><code>AND `post_status` = </code><code>'publish'</code>
</div>
<div>
<code>                  </code><code>AND `meta_key` = </code><code>'_wp_page_template'</code>
</div>
<div>
<code>                  </code><code>AND `meta_value` = %s</code>
</div>
<div>
<code>                  </code><code>LIMIT 1;", </code><code>$template</code><code>));</code>
</div>
<div>
 </div>
<div>
<code>  </code><code>return</code> <code>$page_id</code><code>;</code>
</div>
<div>
<code>}</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
很多人可能会问,获取页面id来干什么?通过id来获取登录页面的链接不行吗:</p>
<div>
<div>
<div id="highlighter_252769">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
<div>
2</div>
<div>
3</div>
</td>
<td>
<div>
<div>
<code>&lt;a href="&lt;?php </code>
</div>
<div>
<code>echo</code> <code>get_permalink(get_page_id_from_template(</code><code>'login.php'</code><code>))</code>
</div>
<div>
<code>?&gt;&gt;登录&lt;/a&gt;</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
也有人会问,通过页面标题和别名不也可以获取到页面id吗?如果主题是给客户用了,你知道客户会用什么样的标题?强制客户用你指定的标题?那你太不人性化了吧!</p>
頁: [1]
查看完整版本: WordPress中获取所使用的模板的页面ID的简单方法