钩子文档

get_template_part_{$slug}

💡 云策文档标注

概述

这是一个 WordPress 动态 Hook,在加载指定模板部分文件之前触发。Hook 名称中的动态部分 {$slug} 对应通用模板部分的 slug 名称。

关键要点

  • Hook 名称:get_template_part_{$slug},其中 $slug 是动态参数,指代模板部分的 slug 名称。
  • 触发时机:在 get_template_part() 函数加载指定模板部分文件之前执行。
  • 参数:包括 $slug(模板部分的 slug 名称)、$name(专用模板名称,可为 null)和 $args(传递给模板的额外参数数组)。
  • 版本历史:从 WordPress 3.0.0 引入,5.5.0 版本添加了 $args 参数。

代码示例

do_action( "get_template_part_{$slug}", $slug, $name, $args );

注意事项

  • 此 Hook 是动态的,开发者需要根据具体 $slug 值来添加或移除动作。
  • 可用于在加载特定模板部分前执行自定义代码,如修改数据或添加条件逻辑。

📄 原文内容

Fires before the specified template part file is loaded.

Description

The dynamic portion of the hook name, $slug, refers to the slug name for the generic template part.

Parameters

$slugstring
The slug name for the generic template.
$namestring|null
The name of the specialized template or null if there is none.
$argsarray
Additional arguments passed to the template.

Source

do_action( "get_template_part_{$slug}", $slug, $name, $args );

Changelog

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