在WordPress模板文件中有很多公共模板,例如头部文件,该函数就是用来引入头部公共文件的,可以减少在每个模板文件中再写一次头部代码。
描述
在WordPress模板文件中有很多公共模板,例如头部文件,该函数就是用来引入头部公共文件的,可以减少在每个模板文件中再写一次头部代码。
本文概括
在本篇文档中小编将为大家讲解一下几点:
- 函数翻译,简单用法
- 不同页面不同头部
- 头部文件传入参数
函数位置
文件:wp-includes / general-template.php 27行
function get_header( $name = null, $args = array() ) { /** * Fires before the header template file is loaded. * * @since 2.1.0 * @since 2.8.0 The `$name` parameter was added. * @since 5.5.0 The `$args` parameter was added. * * @param string|null $name Name of the specific header file to use. Null for the default header. * @param array $args Additional arguments passed to the header template. */ do_action( 'get_header', $name, $args ); $templates = array(); $name = (string) $name; if ( '' !== $name ) { $templates[] = "header-{$name}.php"; } $templates[] = 'header.php'; if ( ! locate_template( $templates, true, true, $args ) ) { return false; } }