get_adjacent_post_rel_link (获取相邻文章链接)

描述

获取相邻文章包含rel标签的链接

语法

get_adjacent_post_rel_link( string $title = '%title', bool $in_same_term = false, array|string $excluded_terms = '', bool $previous = true, string $taxonomy = 'category' )

参数

$title (string) (Optional)(Default value: '%title') title 格式

$in_same_term (bool) (Optional)(Default value: false) 文章是否在同一分类

$excluded_terms (array|string) (Optional)(Default value: '') 排除分类的ID

$previous (bool) (Optional)(Default value: true) 是否是获取上一篇文章链接

$taxonomy (string) (Optional) (Default value: 'category') 如果 $in_same_term 设置为true表示限定的分类

返回值

(string|void) 相邻文章的rel链接

关系

调用

wp-includes/link-template.php: get_adjacent_post()
wp-includes/link-template.php: {$adjacent}_post_rel_link
wp-includes/link-template.php: get_permalink()

被用

wp-includes/link-template.php: adjacent_posts_rel_link()
wp-includes/link-template.php: next_post_rel_link()
wp-includes/link-template.php: prev_post_rel_link()

源码

文件:wp-includes/link-template.php

function get_adjacent_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
    if ( $previous && is_attachment() && $post = get_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'.
     *
     * @since 2.8.0
     *
     * @param string $link The relational link.
     */
    return apply_filters( "{$adjacent}_post_rel_link", $link );
}