我编写了几个函数,在调用之前诱使WordPress认为我们正在上一篇/下一篇文章中previous_post_link
或next_post_link
:
/**
* Displays the next post link that is adjacent to the next post.
*
* @param object $next_post Optional. The next post to reference. Default is current post.
* @param string $format Optional. Link anchor format. Default \'« %link\'.
* @param string $link Optional. Link permalink format. Default \'%title\'
* @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false.
* @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty.
* @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default \'category\'.
*/
function next_next_post_link( $next_post = null, $format = \'%link »\', $link = \'%title\', $in_same_term = false, $excluded_terms = \'\', $taxonomy = \'category\' ) {
global $post;
// Keep track of the current post so we can reset back later.
$current_post = $post;
// If a "next post" is specified, use that.
if ( $next_post ) {
$post = $next_post;
setup_postdata( $post );
}
// Make WordPress think we\'re on the next post.
$post = get_next_post();
setup_postdata( $post );
// Echo the next post link, skipping the next post.
next_post_link( $format, $link, $in_same_term, $excluded_terms, $taxonomy );
// Reset everything back.
$post = $current_post;
wp_reset_postdata();
}
/**
* Displays the previous post link that is adjacent to the previous post.
*
* @param object $previous_post Optional. The previous post to reference. Default is current post.
* @param string $format Optional. Link anchor format. Default \'« %link\'.
* @param string $link Optional. Link permalink format. Default \'%title\'.
* @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false.
* @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty.
* @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default \'category\'.
*/
function previous_previous_post_link( $previous_post = null, $format = \'%link »\', $link = \'%title\', $in_same_term = false, $excluded_terms = \'\', $taxonomy = \'category\' ) {
global $post;
// Keep track of the current post so we can reset back later.
$current_post = $post;
// If a "previous post" is specified, use that.
if ( $previous_post ) {
$post = $previous_post;
setup_postdata( $post );
}
// Make WordPress think we\'re on the previous post.
$post = get_previous_post();
setup_postdata( $post );
// Echo the previous post link, skipping the previous post.
previous_post_link( $format, $link, $in_same_term, $excluded_terms, $taxonomy );
// Reset everything back.
$post = $current_post;
wp_reset_postdata();
}
将这些功能添加到
functions.php
文件,并使用
next_next_post_link
和
previous_previous_post_link
与您使用的方式相同
next_post_link
和
previous_post_link
结果: