_post_format_get_terms()
云策文档标注
概述
_post_format_get_terms() 是一个 WordPress 内部函数,用于在 get_terms() 返回的术语对象中移除 post format 前缀。它处理 post_format 分类法,确保名称属性显示为可读格式。
关键要点
- 函数作用:从 get_terms() 创建的术语对象中移除 post format 前缀,例如将 'post-format-gallery' 转换为 'Gallery'。
- 参数:接受 $terms(术语数组)、$taxonomies(分类法字符串或数组)和 $args(参数数组)作为输入。
- 处理逻辑:检查分类法是否包含 'post_format',根据 $args['fields'] 的值(如 'names')决定处理方式,使用 get_post_format_string() 进行转换。
- 返回值:返回处理后的术语数组。
代码示例
function _post_format_get_terms( $terms, $taxonomies, $args ) {
if ( in_array( 'post_format', (array) $taxonomies, true ) ) {
if ( isset( $args['fields'] ) && 'names' === $args['fields'] ) {
foreach ( $terms as $order => $name ) {
$terms[ $order ] = get_post_format_string( str_replace( 'post-format-', '', $name ) );
}
} else {
foreach ( (array) $terms as $order => $term ) {
if ( isset( $term->taxonomy ) && 'post_format' === $term->taxonomy ) {
$terms[ $order ]->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) );
}
}
}
}
return $terms;
}注意事项
- 此函数是内部函数,通常由 WordPress 核心调用,开发者应避免直接使用,除非有特定需求。
- 依赖于 get_post_format_string() 函数来获取翻译后的格式名称。
- 自 WordPress 3.1.0 版本引入。
原文内容
Remove the post format prefix from the name property of the term objects created by get_terms() .
Parameters
$termsarrayrequired$taxonomiesstring|arrayrequired$argsarrayrequired
Source
function _post_format_get_terms( $terms, $taxonomies, $args ) {
if ( in_array( 'post_format', (array) $taxonomies, true ) ) {
if ( isset( $args['fields'] ) && 'names' === $args['fields'] ) {
foreach ( $terms as $order => $name ) {
$terms[ $order ] = get_post_format_string( str_replace( 'post-format-', '', $name ) );
}
} else {
foreach ( (array) $terms as $order => $term ) {
if ( isset( $term->taxonomy ) && 'post_format' === $term->taxonomy ) {
$terms[ $order ]->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) );
}
}
}
}
return $terms;
}
Changelog
| Version | Description |
|---|---|
| 3.1.0 | Introduced. |