_post_format_wp_get_object_terms()
云策文档标注
概述
_post_format_wp_get_object_terms() 是一个 WordPress 内部函数,用于处理 wp_get_object_terms() 返回的术语对象,移除 post_format 分类中术语名称的前缀。
关键要点
- 该函数遍历 wp_get_object_terms() 返回的术语数组,检查每个术语的 taxonomy 是否为 'post_format'。
- 如果术语属于 post_format 分类,它会使用 get_post_format_string() 函数将术语的 slug 转换为美化后的名称,并更新术语对象的 name 属性。
- 函数返回处理后的术语数组,确保 post_format 术语的名称显示为可读格式。
代码示例
function _post_format_wp_get_object_terms( $terms ) {
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 核心内部函数,通常不应在插件或主题中直接调用,而是通过 wp_get_object_terms() 自动应用。
- 它依赖于 get_post_format_string() 函数来获取翻译后的 post_format 名称,确保国际化支持。
- 自 WordPress 3.1.0 版本引入,用于改进 post_format 术语的显示。
原文内容
Remove the post format prefix from the name property of the term objects created by wp_get_object_terms() .
Parameters
$termsarrayrequired
Source
function _post_format_wp_get_object_terms( $terms ) {
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. |