the_generator()
云策文档标注
概述
the_generator() 函数用于根据请求的输出格式显示生成器 XML 或注释,适用于 RSS、ATOM 等场景。它通过 'the_generator' 过滤器允许插件对生成器输出进行过滤。
关键要点
- 函数返回指定类型的生成器输出,支持类型包括 html、xhtml、atom、rss2、rdf、comment、export。
- 使用 apply_filters('the_generator', ...) 钩子,允许开发者自定义生成器标签的输出。
- 相关函数包括 get_the_generator() 用于创建生成器内容,以及 wp_generator() 在 wp_head 钩子中显示生成器。
代码示例
function the_generator( $type ) {
echo apply_filters( 'the_generator', get_the_generator( $type ), $type ) . "n";
}注意事项
- 参数 $type 为必需,指定生成器类型,需确保传入有效值以避免输出错误。
- 自 WordPress 2.5.0 版本引入,兼容性良好,但建议在插件或主题中使用时检查版本依赖。
原文内容
Displays the generator XML or Comment for RSS, ATOM, etc.
Description
Returns the correct generator type for the requested output format. Allows for a plugin to filter generators overall the ‘the_generator’ filter.
Parameters
$typestringrequired-
The type of generator to output – (
html|xhtml|atom|rss2|rdf|comment|export).
Source
function the_generator( $type ) {
/**
* Filters the output of the XHTML generator tag, for display.
*
* @since 2.5.0
*
* @param string $generator_type The generator output.
* @param string $type The type of generator to output. Accepts 'html',
* 'xhtml', 'atom', 'rss2', 'rdf', 'comment', 'export'.
*/
echo apply_filters( 'the_generator', get_the_generator( $type ), $type ) . "n";
}
Hooks
- apply_filters( ‘the_generator’, string $generator_type, string $type )
-
Filters the output of the XHTML generator tag, for display.
Changelog
| Version | Description |
|---|---|
| 2.5.0 | Introduced. |