返回介绍

is_page_template()

发布于 2017-09-11 01:21:56 字数 4818 浏览 1386 评论 0 收藏 0

is_page_template( string|array $template = '' )

Whether currently in a page template.


description

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.


参数

$template

(string|array) (Optional) The specific template name or array of templates to match.

Default value: ''


返回值

(bool) True on success, false on failure.


源代码

File: wp-includes/post-template.php

function is_page_template( $template = '' ) {
	if ( ! is_singular() ) {
		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 );
}

更新日志

Versiondescription
4.7.0Now works with any post type, not just pages.
4.2.0The $template parameter was changed to also accept an array of page templates.
2.5.0Introduced.

More Information

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.:


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.


  // 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.
  }

相关函数

Uses

  • wp-includes/query.php: is_singular()
  • wp-includes/query.php: get_queried_object_id()
  • wp-includes/post-template.php: get_page_template_slug()

Used By

  • wp-includes/post-template.php: get_body_class()

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 4You must log in to vote on the helpfulness of this note Contributed by Codex

    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.

    
    if ( is_page_template( 'about.php' ) ) {
    	// about.php is used
    } else {
    	// about.php is not used
    }
    
    
  2. If your page template resides within a directory, you can use something like this:

    if ( is_page_template( 'directory-name/page-about.php' ) ) {
        // about.php is used
    } else {
        // about.php is not used
    }

    This can be useful if you’re using multiple page templates and want to keep your files organised.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文