函数文档

get_footer()

💡 云策文档标注

概述

get_footer() 函数用于加载主题的页脚模板文件,支持指定名称以加载专用页脚。它通过参数控制模板选择,并触发相关 Hook。

关键要点

  • 加载默认页脚模板 footer.php,或通过 $name 参数指定如 footer-{$name}.php 的专用模板。
  • 参数 $name 为字符串或 null,默认 null;$args 为数组,用于向模板传递额外参数,默认空数组。
  • 成功时返回 void,模板不存在时返回 false。
  • 触发 get_footer action hook,允许开发者在加载前执行自定义代码。
  • 内部使用 locate_template() 查找模板,确保优先级顺序。

代码示例

// 加载默认页脚
get_footer();

// 加载名为 'special' 的专用页脚,对应文件 footer-special.php
get_footer('special');

// 加载专用页脚并传递额外参数
get_footer('special', array('key' => 'value'));

注意事项

  • 模板文件应位于主题目录中,如 footer.php 或 footer-{$name}.php。
  • 如果指定名称的模板不存在,函数会回退到默认 footer.php。
  • 从 WordPress 5.5.0 版本开始支持 $args 参数,用于向模板传递数据。
  • 常见用例包括为不同页面(如首页、404 页)创建不同页脚,文件命名需遵循约定。

📄 原文内容

Loads footer template.

Description

Includes the footer template for a theme or if a name is specified then a specialized footer will be included.

For the parameter, if the file is called “footer-special.php” then specify “special”.

Parameters

$namestring|nulloptional
The name of the specialized footer.

Default:null

$argsarrayoptional
Additional arguments passed to the footer template.

Default:array()

Return

void|false Void on success, false if the template does not exist.

Source

function get_footer( $name = null, $args = array() ) {
	/**
	 * Fires before the footer 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 footer file to use. Null for the default footer.
	 * @param array       $args Additional arguments passed to the footer template.
	 */
	do_action( 'get_footer', $name, $args );

	$templates = array();
	$name      = (string) $name;
	if ( '' !== $name ) {
		$templates[] = "footer-{$name}.php";
	}

	$templates[] = 'footer.php';

	if ( ! locate_template( $templates, true, true, $args ) ) {
		return false;
	}
}

Hooks

do_action( ‘get_footer’, string|null $name, array $args )

Fires before the footer template file is loaded.

Changelog

Version Description
5.5.0 The $args parameter was added.
1.5.0 Introduced.

User Contributed Notes