返回介绍

load_template()

发布于 2017-09-11 01:41:37 字数 2917 浏览 948 评论 0 收藏 0

load_template( string $_template_file,  bool $require_once = true )

Require the template file with WordPress environment.


description

The globals are set up for the template file to ensure that the WordPress environment is available from within the function. The query variables are also available.


参数

$_template_file

(string) (Required) Path to template file.

$require_once

(bool) (Optional) Whether to require_once or require.

Default value: true


源代码

File: wp-includes/template.php

function load_template( $_template_file, $require_once = true ) {
	global $posts, $post, $wp_did_header, $wp_query, $wp_rewrite, $wpdb, $wp_version, $wp, $id, $comment, $user_ID;

	if ( is_array( $wp_query->query_vars ) ) {
		extract( $wp_query->query_vars, EXTR_SKIP );
	}

	if ( isset( $s ) ) {
		$s = esc_attr( $s );
	}

	if ( $require_once ) {
		require_once( $_template_file );
	} else {
		require( $_template_file );
	}
}

更新日志

Versiondescription
1.5.0Introduced.

相关函数

Uses

  • wp-includes/formatting.php: esc_attr()

Used By

  • wp-includes/functions.php: do_feed_rdf()
  • wp-includes/functions.php: do_feed_rss()
  • wp-includes/functions.php: do_feed_rss2()
  • wp-includes/functions.php: do_feed_atom()
  • wp-includes/template.php: locate_template()

User Contributed Notes

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

    Loading a template in a plugin, but allowing theme and child theme to override template

    
    if ( $overridden_template = locate_template( 'some-template.php' ) ) {
    	/*
    	 * locate_template() returns path to file.
    	 * if either the child theme or the parent theme have overridden the template.
    	 */
    	load_template( $overridden_template );
    } else {
    	/*
    	 * If neither the child nor parent theme have overridden the template,
    	 * we load the template from the 'templates' sub-directory of the directory this file is in.
    	 */
    	load_template( dirname( __FILE__ ) . '/templates/some-template.php' );
    }
    

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

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

发布评论

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