修改一个博客页面,当按下一个/前一个链接时,只显示同一类别的文章


Modifying a blog page to only display posts from same category when next/prev link is pressed

我看过一篇非常类似的文章,还有很多其他的例子,比如这里的自定义函数的wordpress文档:下一个/上一个帖子链接的函数参考

然而,我的示例位于Storefront (woothemes)主题内的自定义函数中,因此我在以正确的方式合并"$in_same_term = true"时遇到了麻烦,而不会破坏功能。这里是(原始)代码位在主题内的'post.php'文件中,我认为我需要以某种方式合并"$in_same_term = true,"…

    if ( ! function_exists( 'storefront_paging_nav' ) ) {
/**
 * Display navigation to next/previous set of posts when applicable.
 */
function storefront_paging_nav() {
    global $wp_query;
        $args = array(
        'type'      => 'list',
        'next_text' => _x( 'Next', 'Next post', 'storefront' ) . '&nbsp;<span class="meta-nav">&rarr;</span>',
        'prev_text' => '<span class="meta-nav">&larr;</span>&nbsp' . _x( 'Previous', 'Previous post', 'storefront' ),
        );
    the_posts_pagination( $args );
}
    if ( ! function_exists( 'storefront_post_nav' ) ) {
/**
 * Display navigation to next/previous post when applicable.
 */
function storefront_post_nav() {
    $args = array(
        'next_text' => '%title &nbsp;<span class="meta-nav">&rarr;</span>',
        'prev_text' => '<span class="meta-nav">&larr;</span>&nbsp;%title',
        );
    the_post_navigation( $args );
}
    }

我想我可以接近,因为大量的搜索都显示了同样的信息,我只是没有正确地整合它…

提前感谢任何关于如何最好地整合此功能的建议!

您只需将'in_same_term' => true添加到storefont_post_nav函数的数组

function storefront_post_nav() {
    $args = array(
        'next_text' => '%title &nbsp;<span class="meta-nav">&rarr;</span>',
        'prev_text' => '<span class="meta-nav">&larr;</span>&nbsp;%title',
        'in_same_term' => true,   
    );
}

可以在数组中添加其他选项。你可以在这里看看https://developer.wordpress.org/reference/functions/the_post_navigation/

好的,所以我这里的主要问题是试图在错误的地方合并代码-我所要做的就是改变这个文件中的next_post_link参数:wp-includes/link-template.php。(不是post.php文件,因为我试图这样做)。

值得注意的是,对于每个下一个和上一个链接,有两组参数需要将$in_same_term = false更改为true

现在效果很好!