previous_post()
云策文档标注
概述
previous_post() 是一个已弃用的 WordPress 函数,用于输出指向上一篇文章的链接。它已被 previous_post_link() 替代,开发者应避免使用此函数。
关键要点
- 函数已弃用:自 WordPress 2.0.0 起,previous_post() 被标记为弃用,推荐使用 previous_post_link()。
- 功能:输出上一篇文章的链接,支持自定义格式、标题显示和分类限制。
- 参数:包括 $format(格式字符串)、$previous(链接前缀)、$title(是否显示标题)、$in_same_cat(是否限制在同一分类)、$limitprev(限制数量)和 $excluded_categories(排除分类)。
- 内部调用:依赖于 get_previous_post() 获取文章数据,使用 get_permalink() 生成链接,并通过 apply_filters('the_title') 过滤标题。
代码示例
function previous_post($format='%', $previous='previous post: ', $title='yes', $in_same_cat='no', $limitprev=1, $excluded_categories='') {
_deprecated_function( __FUNCTION__, '2.0.0', 'previous_post_link()' );
if ( empty($in_same_cat) || 'no' == $in_same_cat )
$in_same_cat = false;
else
$in_same_cat = true;
$post = get_previous_post($in_same_cat, $excluded_categories);
if ( !$post )
return;
$string = '<a href="' . get_permalink($post->ID) . '">' . $previous;
if ( 'yes' == $title )
$string .= apply_filters('the_title', $post->post_title, $post->ID);
$string .= '</a>';
$format = str_replace('%', $string, $format);
echo $format;
}注意事项
- 弃用警告:使用此函数会触发 _deprecated_function() 警告,建议更新代码以使用 previous_post_link()。
- 参数处理:$in_same_cat 参数接受字符串 'yes' 或 'no',内部转换为布尔值。
- 兼容性:函数在 WordPress 1.5.0 中引入,2.0.0 中弃用,需注意版本兼容性。
原文内容
Prints a link to the previous post.
Description
See also
Parameters
$formatstringrequired$previousstringrequired$titlestringrequired$in_same_catstringrequired$limitprevintoptional-
Default:
1 $excluded_categoriesstringrequired
Source
function previous_post($format='%', $previous='previous post: ', $title='yes', $in_same_cat='no', $limitprev=1, $excluded_categories='') {
_deprecated_function( __FUNCTION__, '2.0.0', 'previous_post_link()' );
if ( empty($in_same_cat) || 'no' == $in_same_cat )
$in_same_cat = false;
else
$in_same_cat = true;
$post = get_previous_post($in_same_cat, $excluded_categories);
if ( !$post )
return;
$string = '<a href="'.get_permalink($post->ID).'">'.$previous;
if ( 'yes' == $title )
$string .= apply_filters('the_title', $post->post_title, $post->ID);
$string .= '</a>';
$format = str_replace('%', $string, $format);
echo $format;
}
Hooks
- apply_filters( ‘the_title’, string $post_title, int $post_id )
-
Filters the post title.
Changelog
| Version | Description |
|---|---|
| 2.0.0 | Deprecated. Use previous_post_link() |
| 1.5.0 | Introduced. |