get_parent_post_rel_link()
云策文档标注
概述
get_parent_post_rel_link() 是一个已弃用的 WordPress 函数,用于获取父文章的关联链接。它生成一个包含标题和日期的链接,但自 WordPress 3.3.0 起不再推荐使用。
关键要点
- 函数已弃用:自 WordPress 3.3.0 版本起标记为弃用,建议使用替代方法。
- 参数:可选参数 $title 用于指定链接标题格式,默认值为 '%title',支持占位符替换。
- 返回值:返回一个字符串形式的 HTML 链接,或为空(如果父文章不存在)。
- 内部逻辑:检查全局 $post 对象和父文章 ID,使用 get_post() 获取父文章数据,并应用过滤器进行标题处理。
- 相关 Hook:应用 the_title 过滤器来修改链接标题。
代码示例
function get_parent_post_rel_link( $title = '%title' ) {
_deprecated_function( __FUNCTION__, '3.3.0' );
if ( ! empty( $GLOBALS['post'] ) && ! empty( $GLOBALS['post']->post_parent ) )
$post = get_post($GLOBALS['post']->post_parent);
if ( empty($post) )
return;
$date = mysql2date(get_option('date_format'), $post->post_date);
$title = str_replace('%title', $post->post_title, $title);
$title = str_replace('%date', $date, $title);
$title = apply_filters('the_title', $title, $post->ID);
$link = "n";
return apply_filters( "parent_post_rel_link", $link );
}注意事项
- 弃用警告:调用此函数会触发 _deprecated_function(),在开发环境中可能显示警告信息。
- 替代方案:考虑使用其他 WordPress 核心函数或自定义方法来实现类似功能,以避免依赖弃用代码。
- 兼容性:在维护旧主题或插件时需注意此函数已弃用,可能影响未来版本的兼容性。
原文内容
Get parent post relational link.
Parameters
$titlestringoptional-
Link title format. Default
'%title'.
Source
function get_parent_post_rel_link( $title = '%title' ) {
_deprecated_function( __FUNCTION__, '3.3.0' );
if ( ! empty( $GLOBALS['post'] ) && ! empty( $GLOBALS['post']->post_parent ) )
$post = get_post($GLOBALS['post']->post_parent);
if ( empty($post) )
return;
$date = mysql2date(get_option('date_format'), $post->post_date);
$title = str_replace('%title', $post->post_title, $title);
$title = str_replace('%date', $date, $title);
$title = apply_filters('the_title', $title, $post->ID);
$link = "<link rel='up' title='";
$link .= esc_attr( $title );
$link .= "' href='" . get_permalink($post) . "' />n";
return apply_filters( "parent_post_rel_link", $link );
}
Hooks
- apply_filters( ‘the_title’, string $post_title, int $post_id )
-
Filters the post title.