函数文档

next_post()

💡 云策文档标注

概述

next_post() 是一个已弃用的 WordPress 函数,用于输出指向下一篇文章的链接。它已被 next_post_link() 替代,开发者应避免使用此函数。

关键要点

  • next_post() 函数已从 WordPress 2.0.0 版本起弃用,建议使用 next_post_link() 替代。
  • 函数接受多个参数,包括 $format、$next、$title、$in_same_cat、$limitnext 和 $excluded_categories,用于自定义链接输出。
  • 内部调用 get_next_post() 获取相邻文章,并应用 the_title 过滤器处理文章标题。
  • 相关函数包括 get_next_post()、_deprecated_function()、get_permalink() 和 apply_filters()。

代码示例

function next_post($format='%', $next='next post: ', $title='yes', $in_same_cat='no', $limitnext=1, $excluded_categories='') {
    _deprecated_function( __FUNCTION__, '2.0.0', 'next_post_link()' );

    if ( empty($in_same_cat) || 'no' == $in_same_cat )
        $in_same_cat = false;
    else
        $in_same_cat = true;

    $post = get_next_post($in_same_cat, $excluded_categories);

    if ( !$post )
        return;

    $string = '<a href="' . get_permalink($post->ID) . '">' . $next;
    if ( 'yes' == $title )
        $string .= apply_filters('the_title', $post->post_title, $post->ID);
    $string .= '</a>';
    $format = str_replace('%', $string, $format);
    echo $format;
}

注意事项

  • 由于此函数已弃用,新开发中应使用 next_post_link() 以避免兼容性问题。
  • 参数 $in_same_cat 和 $title 接受字符串值,但函数内部会进行布尔转换或条件判断。
  • 如果未找到下一篇文章,函数会静默返回,不输出任何内容。

📄 原文内容

Prints link to the next post.

Description

See also

Parameters

$formatstringrequired
$nextstringrequired
$titlestringrequired
$in_same_catstringrequired
$limitnextintoptional

Default:1

$excluded_categoriesstringrequired

Source

function next_post($format='%', $next='next post: ', $title='yes', $in_same_cat='no', $limitnext=1, $excluded_categories='') {
	_deprecated_function( __FUNCTION__, '2.0.0', 'next_post_link()' );

	if ( empty($in_same_cat) || 'no' == $in_same_cat )
		$in_same_cat = false;
	else
		$in_same_cat = true;

	$post = get_next_post($in_same_cat, $excluded_categories);

	if ( !$post	)
		return;

	$string = '<a href="'.get_permalink($post->ID).'">'.$next;
	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 next_post_link()
0.71 Introduced.