函数文档

get_adjacent_post_rel_link()

💡 云策文档标注

概述

get_adjacent_post_rel_link() 函数用于获取相邻文章的关联链接,支持前一篇或后一篇。它基于 get_adjacent_post() 获取相邻文章,并生成包含标题和日期的链接,可通过 Hook 进行过滤。

关键要点

  • 函数返回相邻文章的关联链接 URL,类型为字符串或 void(无返回值)。
  • 参数包括 $title(链接标题格式,默认 '%title')、$in_same_term(是否在同一分类术语中,默认 false)、$excluded_terms(排除的术语 ID 数组或字符串,默认空)、$previous(是否获取前一篇链接,默认 true)、$taxonomy(分类法,默认 'category')。
  • 内部使用 get_adjacent_post() 获取相邻文章,并处理附件页面的特殊情况。
  • 支持 Hook:apply_filters("{$adjacent}_post_rel_link", $link),其中 $adjacent 为 'next' 或 'previous',用于过滤链接。
  • 相关函数包括 is_attachment()、mysql2date()、the_title_attribute()、__()、esc_attr()、get_permalink()、apply_filters()、get_option()、get_post() 等。

代码示例

function get_adjacent_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
    $post = get_post();
    if ( $previous && is_attachment() && $post ) {
        $post = get_post( $post->post_parent );
    } else {
        $post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy );
    }

    if ( empty( $post ) ) {
        return;
    }

    $post_title = the_title_attribute(
        array(
            'echo' => false,
            'post' => $post,
        )
    );

    if ( empty( $post_title ) ) {
        $post_title = $previous ? __( 'Previous Post' ) : __( 'Next Post' );
    }

    $date = mysql2date( get_option( 'date_format' ), $post->post_date );

    $title = str_replace( '%title', $post_title, $title );
    $title = str_replace( '%date', $date, $title );

    $link  = $previous ? "
" : "
";

    $adjacent = $previous ? 'previous' : 'next';

    return apply_filters( "{$adjacent}_post_rel_link", $link );
}

注意事项

  • 函数在 WordPress 2.8.0 版本引入,用于生成相邻文章的关联链接,常用于文章导航。
  • 如果 $in_same_term 为 true,需指定 $taxonomy 参数以确定分类法,否则默认使用 'category'。
  • 当 $previous 为 true 且当前页面为附件时,函数会获取附件父文章的相邻链接。
  • 链接标题支持占位符 %title 和 %date,分别替换为文章标题和日期。
  • 返回的链接已通过 esc_attr() 转义,确保 HTML 属性安全。

📄 原文内容

Retrieves the adjacent post relational link.

Description

Can either be next or previous post relational link.

Parameters

$titlestringoptional
Link title format. Default '%title'.
$in_same_termbooloptional
Whether link should be in the same taxonomy term.

Default:false

$excluded_termsint[]|stringoptional
Array or comma-separated list of excluded term IDs.
Default empty.
$previousbooloptional
Whether to display link to previous or next post.

Default:true

$taxonomystringoptional
Taxonomy, if $in_same_term is true. Default 'category'.

Return

string|void The adjacent post relational link URL.

Source

function get_adjacent_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
	$post = get_post();
	if ( $previous && is_attachment() && $post ) {
		$post = get_post( $post->post_parent );
	} else {
		$post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy );
	}

	if ( empty( $post ) ) {
		return;
	}

	$post_title = the_title_attribute(
		array(
			'echo' => false,
			'post' => $post,
		)
	);

	if ( empty( $post_title ) ) {
		$post_title = $previous ? __( 'Previous Post' ) : __( 'Next Post' );
	}

	$date = mysql2date( get_option( 'date_format' ), $post->post_date );

	$title = str_replace( '%title', $post_title, $title );
	$title = str_replace( '%date', $date, $title );

	$link  = $previous ? "<link rel='prev' title='" : "<link rel='next' title='";
	$link .= esc_attr( $title );
	$link .= "' href='" . get_permalink( $post ) . "' />n";

	$adjacent = $previous ? 'previous' : 'next';

	/**
	 * Filters the adjacent post relational link.
	 *
	 * The dynamic portion of the hook name, `$adjacent`, refers to the type
	 * of adjacency, 'next' or 'previous'.
	 *
	 * Possible hook names include:
	 *
	 *  - `next_post_rel_link`
	 *  - `previous_post_rel_link`
	 *
	 * @since 2.8.0
	 *
	 * @param string $link The relational link.
	 */
	return apply_filters( "{$adjacent}_post_rel_link", $link );
}

Hooks

apply_filters( “{$adjacent}_post_rel_link”, string $link )

Filters the adjacent post relational link.

Changelog

Version Description
2.8.0 Introduced.