函数文档

get_boundary_post_rel_link()

💡 云策文档标注

概述

get_boundary_post_rel_link() 是一个已弃用的 WordPress 函数,用于获取边界文章(第一篇或最后一篇文章)的关系链接。它基于 get_boundary_post() 获取文章,并应用过滤器生成链接。

关键要点

  • 函数已弃用:自 WordPress 3.3.0 版本起被标记为弃用,建议使用替代方法。
  • 功能:生成指向第一篇或最后一篇文章的链接,支持自定义标题格式和分类限制。
  • 参数:包括 $title(标题格式)、$in_same_cat(是否在同一分类)、$excluded_categories(排除的分类 ID)和 $start(是否为首篇文章)。
  • 返回值:返回字符串形式的链接,或为空。
  • 内部逻辑:调用 get_boundary_post() 获取文章,处理标题和日期,并应用 the_title 和边界链接过滤器。

代码示例

function get_boundary_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '', $start = true) {
    _deprecated_function( __FUNCTION__, '3.3.0' );

    $posts = get_boundary_post($in_same_cat, $excluded_categories, $start);
    // If there is no post, stop.
    if ( empty($posts) )
        return;

    // Even though we limited get_posts() to return only 1 item it still returns an array of objects.
    $post = $posts[0];

    if ( empty($post->post_title) )
        $post->post_title = $start ? __('First Post') : __('Last Post');

    $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 = $start ? "n" : "n";

    $boundary = $start ? 'start' : 'end';
    return apply_filters( "{$boundary}_post_rel_link", $link );
}

注意事项

  • 弃用状态:此函数已弃用,开发者应避免在新代码中使用,并考虑替代方案如自定义查询。
  • 依赖函数:内部使用 get_boundary_post()、mysql2date()、apply_filters() 等核心函数,需确保这些函数可用。
  • 过滤器应用:链接生成过程中应用了 the_title 和边界特定过滤器,允许自定义输出。

📄 原文内容

Get boundary post relational link.

Description

Can either be start or end post relational link.

Parameters

$titlestringoptional
Link title format. Default '%title'.
$in_same_catbooloptional
Whether link should be in a same category.

Default:false

$excluded_categoriesstringoptional
Excluded categories IDs. Default empty.
$startbooloptional
Whether to display link to first or last post.

Default:true

Return

string

Source

function get_boundary_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '', $start = true) {
	_deprecated_function( __FUNCTION__, '3.3.0' );

	$posts = get_boundary_post($in_same_cat, $excluded_categories, $start);
	// If there is no post, stop.
	if ( empty($posts) )
		return;

	// Even though we limited get_posts() to return only 1 item it still returns an array of objects.
	$post = $posts[0];

	if ( empty($post->post_title) )
		$post->post_title = $start ? __('First Post') : __('Last Post');

	$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 = $start ? "<link rel='start' title='" : "<link rel='end' title='";
	$link .= esc_attr($title);
	$link .= "' href='" . get_permalink($post) . "' />n";

	$boundary = $start ? 'start' : 'end';
	return apply_filters( "{$boundary}_post_rel_link", $link );
}

Hooks

apply_filters( ‘the_title’, string $post_title, int $post_id )

Filters the post title.

Changelog

Version Description
3.3.0 Deprecated.
2.8.0 Introduced.