钩子文档

get_footer

💡 云策文档标注

概述

get_footer 是一个 WordPress Hook,在 get_footer() 函数调用开始时触发,用于在加载页脚模板文件之前执行代码。它允许开发者根据特定页脚文件名称定制操作。

关键要点

  • Hook 名称:get_footer,在 get_footer() 函数开始时触发。
  • 参数:$name(字符串或 null,指定页脚文件名称,默认为 null 使用默认页脚),$args(数组,传递给页脚模板的额外参数)。
  • 用途:适合设置和执行不会立即输出到浏览器的代码,避免在页面标记显示前输出内容。
  • 添加方式:通过 add_action 在 functions.php 文件中添加动作。
  • 版本历史:$args 参数在 5.5.0 版本添加,$name 参数在 2.8.0 版本添加,Hook 在 2.1.0 版本引入。

代码示例

function themeslug_footer_hook( $name ) {
    if ( 'new' == $name ) {
        // 条件性显示脚本,例如针对 footer-new.php 模板
    }
}

注意事项

此 Hook 最适合用于设置和执行不会立即回显到浏览器的代码,任何回显内容都会在页面标记显示之前出现。


📄 原文内容

Fires before the footer template file is loaded.

Parameters

$namestring|null
Name of the specific footer file to use. Null for the default footer.
$argsarray
Additional arguments passed to the footer template.

More Information

get_footer is a hook that gets run at the very start of the get_footer() function call. If you pass in the name for a specific footer file, like get_footer( 'new' ), the do_action will pass in the name as a parameter for the hook. This allows you to limit your add_action calls to specific templates if you wish. Actions added to this hook should be added to your functions.php file.

Note: This hook is best to use to set up and execute code that doesn’t get echoed to the browser until later in the page load. Anything you echo will show up before any of the markup is displayed.

Source

do_action( 'get_footer', $name, $args );

Changelog

Version Description
5.5.0 The $args parameter was added.
2.8.0 The $name parameter was added.
2.1.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    The following example will display a script conditionally for a different footer. This is just one example of how you may use the hook and will use a secondary template file of footer-new.php

    function themeslug_footer_hook( $name ) {
    	if ( 'new' == $name ) { ?>
    		
    			(function($) {
    				//put all your jQuery goodness in here.
    			})(jQuery);