get_adjacent_post_rel_link

函数
get_adjacent_post_rel_link ( $title = '%title', $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' )
参数
  • (string) $title Optional. Link title format. Default '%title'.
    Required:
    Default: '%title'
  • (bool) $in_same_term Optional. Whether link should be in a same taxonomy term. Default false.
    Required:
    Default: false
  • (int[]|string) $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty.
    Required:
    Default: (empty)
  • (bool) $previous Optional. Whether to display link to previous or next post. Default true.
    Required:
    Default: true
  • (string) $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
    Required:
    Default: 'category'
返回值
  • (string|void) The adjacent post relational link URL.
定义位置
相关方法
get_adjacent_post_linkadjacent_posts_rel_linkget_parent_post_rel_linkadjacent_post_linkget_adjacent_post
引入
2.8.0
弃用
-

get_adjacent_post_rel_link: 这个函数用来获取与当前文章相关的下一个或上一个文章的链接,使用rel属性。

检索相邻文章的关系链接。

可以是下一个或上一个文章的关系链接。

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 );
}

常见问题

FAQs
查看更多 >