目录
描述
This Conditional Tag allows you to determine if you are in any page template. Optionally checks if a specific Page Template is being used in a Page. This is a boolean function, meaning it returns either TRUE or FALSE. This tag must be used BEFORE The Loop and does not work inside The Loop (see Notes below).
用法
<?php is_page_template( $template ); ?>
参数
$template
(string|array) (可选) The specific template name or array of templates to match.
默认值: None
返回值
示例
Is Page Template 'about' being used? Note that unlike with other conditionals, if you want to specify a particular Page Template, you need to use the filename, such as about.php or my_page_template.php.
1 2 3 4 5 6 7 8 9 |
/* ---------------------------------- * wordpress之魂 © http://wphun.com * ---------------------------------- */ if ( is_page_template( 'about.php' ) ) { // Returns true when 'about.php' is being used. } else { // Returns false when 'about.php' is not being used. } |
注意
Page template in subdirectory
If the page template is located in a subdirectory of the theme (since WP 3.4), prepend the folder name and a slash to the template filename, e.g.:
1 2 3 4 5 |
/* ---------------------------------- * wordpress之魂 © http://wphun.com * ---------------------------------- */ is_page_template( 'templates/about.php' ); |
Cannot Be Used Inside The Loop
Due to certain global variables being overwritten during The Loop is_page_template()
will not work. In order to use it after The Loop you must call wp_reset_query() after The Loop.
Alternative
Since the page template slug is stored inside the post_meta for any post that has been assigned to a page template, it is possible to directly query the post_meta to see whether any given page has been assigned a page template. This is the method that is_page_template() uses internally.
The function get_page_template_slug( $post_id ) will return the slug of the currently assigned page template (or an empty string if no template has been assigned - or false if the $post_id does not correspond to an actual page). You can easily use this anywhere (in The Loop, or outside) to determine whether any page has been assigned a page template.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
/* ---------------------------------- * wordpress之魂 © http://wphun.com * ---------------------------------- */ <?php // in the loop: if ( get_page_template_slug( get_the_ID() ) ){ // Yep, this page has a page template } // anywhere: if ( get_page_template_slug( $some_post_ID ) ){ // Uh-huh. } ?> |
- See also is_page()
历史
添加于 版本 2.5.0
源文件
is_page_template() 函数的代码位于 wp-includes/post-template.php
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
/* ---------------------------------- * wordpress之魂 © http://wphun.com * ---------------------------------- */ /** * Whether currently in a page template. * * This template tag allows you to determine if you are in a page template. * You can optionally provide a template name or array of template names * and then the check will be specific to that template. * * @since 2.5.0 * @since 4.2.0 The `$template` parameter was changed to also accept an array of page templates. * * @param string|array $template The specific template name or array of templates to match. * @return bool True on success, false on failure. */ function is_page_template( $template = '' ) { if ( ! is_page() ) return false; $page_template = get_page_template_slug( get_queried_object_id() ); if ( empty( $template ) ) return (bool) $page_template; if ( $template == $page_template ) return true; if ( is_array( $template ) ) { if ( ( in_array( 'default', $template, true ) && ! $page_template ) || in_array( $page_template, $template, true ) ) { return true; } } return ( 'default' === $template && ! $page_template ); } |
- 原文:http://codex.wordpress.org/Function_Reference/is_page_template
- 翻译:黄聪@WordPress之魂