钩子文档

theme_{$post_type}_templates

💡 云策文档标注

概述

theme_{$post_type}_templates 是一个 WordPress 过滤器钩子,用于过滤主题的页面模板列表。它允许开发者根据文章类型动态修改可用的模板选项。

关键要点

  • 钩子名称是动态的,基于 $post_type 参数,例如 theme_post_templates、theme_page_templates。
  • 参数包括 $post_templates(模板数组)、$theme(主题对象)、$post(文章对象或 null)和 $post_type(文章类型)。
  • 主要用于 WP_Theme::get_page_templates() 方法中,控制模板选择。

注意事项

  • 从 WordPress 4.7.0 开始添加了 $post_type 参数,增强了灵活性。
  • 钩子自 3.9.0 版本引入,4.4.0 版本后允许完全控制 $page_templates 数组。

📄 原文内容

Filters list of page templates for a theme.

Description

The dynamic portion of the hook name, $post_type, refers to the post type.

Possible hook names include:

  • theme_post_templates
  • theme_page_templates
  • theme_attachment_templates

Parameters

$post_templatesstring[]
Array of template header names keyed by the template file name.
$themeWP_Theme
The theme object.
$postWP_Post|null
The post being edited, provided for context, or null.
$post_typestring
Post type to get the templates for.

Source

$post_templates = (array) apply_filters( "theme_{$post_type}_templates", $post_templates, $this, $post, $post_type );

Changelog

Version Description
4.7.0 Added the $post_type parameter.
4.4.0 Converted to allow complete control over the $page_templates array.
3.9.0 Introduced.