Make`pre上_post_link()`函数显示下一个帖子,即跳过一个帖子

时间:2020-08-11 作者:The Chewy

使用previous_post_link() 函数使其在下一篇文章之后获取上一篇文章,即使其跳过一篇文章。那么,如果你在第5栏,按数字顺序,它会跳到第3栏?

原因是我有自定义的帖子类型single-cpt.php 文件中包含两篇文章,因此,当使用前一篇文章链接时,意味着当你转到下一篇文章时,前一篇文章中的一篇又出现了。

任何帮助都是难以置信的。

2 个回复
最合适的回答,由SO网友:Alan 整理而成

$current_id = get_the_ID();
$cpt = get_post_type();
$all_ids = get_posts(array(
    \'fields\'          => \'ids\',
    \'posts_per_page\'  => -1,
    \'post_type\' => $cpt,
    \'order_by\' => \'post_date\',
    \'order\' => \'ASC\',
));
$prev_key = array_search($current_id, $all_ids) - 2;
$next_key = array_search($current_id, $all_ids) + 2;
if(array_key_exists($prev_key, $all_ids)){
    $prev_link = get_the_permalink($all_ids[$prev_key]);
    echo $prev_link;
}
if(array_key_exists($next_key, $all_ids)){
    $next_link = get_the_permalink($all_ids[$next_key]);
    echo $next_link;
}
因此,我查询了当前帖子类型中的所有帖子ID。因为它是一个简单的数组键=>;值,只需在数组中找到当前帖子键,然后进行加减,因此如果当前帖子是第8篇,则下一篇是10篇,上一篇是6篇。然后,如果你的数组没有足够的密钥,假设你当前的是第8个,但你的数组只有9个,这会把它搞砸,我会检查密钥是否存在。如果是,请将get\\u the\\u permalink()与所需键的值一起使用。

可能不是最优雅的方式,但…

SO网友:Nate Allen

我编写了几个函数,在调用之前诱使WordPress认为我们正在上一篇/下一篇文章中previous_post_linknext_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_linkprevious_previous_post_link 与您使用的方式相同next_post_linkprevious_post_link

结果:screenshot showing post #3 with links to post #1 and post #5

相关推荐

是否可以更改GET_POSTS()或wp_Query()函数来更改返回的结果?

我有一个有点难的问题。是否可以更改get\\u posts()或wp\\u query()函数来更改返回的结果?例如,您想在列表中显示某些特定元数据低于其他页面的帖子/页面,您想在列表中显示帖子类型“custom”低于其他页面的帖子(是否有挂钩:)?