使用下一个/上一个帖子链接显示同一类别的帖子

时间:2014-06-17 作者:user54318

我正在使用此代码,但一旦单击下一篇文章/上一篇文章链接,我就会被重定向到另一个类别的下一篇文章/上一篇文章。

 previous_post_link( \'%link\', \'Prev post in category\', $in_same_term = true );
 next_post_link( \'%link\', \'Next post in category\', $in_same_term = true );
我正在尝试使用this article.

以下是我用于帖子和类别的代码,我没有使用自定义帖子类型和类别:

$post_id = $post->ID;
$cat = get_the_category();
$current_cat_id = $cat[0]->cat_ID;
$args = array( 
    \'category\' => $current_cat_id, 
    \'orderby\'  => \'post_date\', 
    \'order\'    => \'DESC\' 
);
$posts = get_posts( $args );
foreach( $posts as $post ) {
    echo $post->post_content;
}
previous_post_link( \'%link\', \'Prev post in category\', $in_same_term = true );
next_post_link( \'%link\', \'Next post in category\', $in_same_term = true );
它基本上是基于类别获取所有帖子,现在我希望下一篇帖子/上一篇帖子的链接只适用于这个特定类别。

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

以下是获取帖子上基于类别的上一个和下一个链接的代码:

<?php
$post_id = $post->ID; // current post ID
$cat = get_the_category(); 
$current_cat_id = $cat[0]->cat_ID; // current category ID 

$args = array( 
    \'category\' => $current_cat_id,
    \'orderby\'  => \'post_date\',
    \'order\'    => \'DESC\'
);
$posts = get_posts( $args );
// get IDs of posts retrieved from get_posts
$ids = array();
foreach ( $posts as $thepost ) {
    $ids[] = $thepost->ID;
}
// get and echo previous and next post in the same category
$thisindex = array_search( $post_id, $ids );
$previd    = isset( $ids[ $thisindex - 1 ] ) ? $ids[ $thisindex - 1 ] : false;
$nextid    = isset( $ids[ $thisindex + 1 ] ) ? $ids[ $thisindex + 1 ] : false;

if (false !== $previd ) {
    ?><a rel="prev" href="<?php echo get_permalink($previd) ?>">Previous</a><?php
}
if (false !== $nextid ) {
    ?><a rel="next" href="<?php echo get_permalink($nextid) ?>">Next</a><?php
}

SO网友:engelen

这个previous_post_linknext_post_link 函数都有五个参数:$format: 设置链接的字符串格式,用于控制链接前后的内容
$link: 链接文本以显示
$in_same_term: 下一篇/上一篇文章是否必须与当前文章在相同的分类术语中
$excluded_terms: 从中排除职位的条款$taxonomy: 在以下情况下使用的分类法$in_same_term 正如你所见$in_same_term 参数正好满足您的需要。但是,您在示例代码中没有正确使用它。实际上,您正在传递分配的结果true 到变量$in_same_term. 这行不通:传递参数就像传递值一样简单:

previous_post_link( \'%link\', \'Prev post in category\', true );
next_post_link( \'%link\', \'Next post in category\', true );
Edit: (OP更新问题后编辑)问题是previous_post_linknext_post_link 使用要覆盖的全局post对象。要防止出现这种情况,请在$posts-循环,例如$singlepost:

foreach ( $posts as $singlepost ) {
    echo $singlepost->post_content
}
这样,全球$post 对象被保留。或者,可以将全局post对象存储在临时变量中并重置$post 稍后再说,但只有在你打电话的时候才有必要setup_postdata (你不是)。

SO网友:Pieter Goosen

你的代码对我来说没有意义,除了有语法错误。正如您的代码所示,当您从帖子/博客页面单击帖子时,您将被带到该帖子的单一视图。只有那篇文章显示在单条上。php。

当你点击帖子链接时,问题就开始了,不管是上一个还是下一个帖子链接。当加载下一页/上一页时,返回的是该特定类别中的所有帖子。这就是你的单曲编码方式。php,以及为什么您的帖子链接不能按预期工作。

我不会使用get_posts() 在单曲上设置我的循环。php页面。我只会使用正常的正确循环。请查看法典中关于Theme Development

这里是一个单一的例子。将按预期工作的php

<?php
get_header(); ?>

<div id="main-content" class="main-content">

    <div id="primary" class="content-area">
        <div id="content" class="site-content" role="main">
            <?php
                // Start the Loop.
                while ( have_posts() ) : the_post(); ?>

            <?php
                    get_template_part( \'content\', get_post_format() );

                    // Previous/next post navigation.
                    previous_post_link( \'%link\', \'Prev post in category\', true );
                    next_post_link( \'%link\', \'Next post in category\', true );

                    // If comments are open or we have at least one comment, load up the comment template.
                    if ( comments_open() || get_comments_number() ) {
                        comments_template();
                    }
                endwhile;
            ?>
        </div><!-- #content -->
    </div><!-- #primary -->

<?php get_sidebar( \'content\' ); ?>
</div><!-- #main-content -->

<?php
get_footer();
正如另一个答案所指出的,去阅读一下如何使用next_post_link 以及previous_post_link

SO网友:honk31

我也有同样的问题,而且我需要定制帖子类型所需的一切&;自定义分类法。user54318为我指出了正确的方向,因为它无法运行cpt,所以我也将在这里分享我的结果:

//get custom taxonomies.
$terms = wp_get_post_terms( get_the_ID(), \'product_cat\' ); //last argument is the custom taxonomy. change to desired tax
//run through all terms and filter out the one, that i need. 
$stay_in = array();
foreach( $terms as $term ) :
/*this loop looks for a category, that is a children of category id 37. change to your needs. 
only importance is to build an array of term ids, to be included in the previous/next behaviour, so if you already know your ids, you could also use something like $stay_in = array( 43 ); and skip this whole loop..*/
    if ( $term->parent == 37 ) :
        $stay_in[] = $term->term_id;
        break; //break out the foreach, if found.
    endif;
endforeach;
//get all post ids, that are in my defined category
$args = array(
    \'post_type\'         => \'product\', //custom post type
    \'posts_per_page\'    => -1,
    \'tax_query\'         => array(
        array(
            \'taxonomy\'  => \'product_cat\', // custom taxonomy
            \'field\'     => \'term_id\',
            \'terms\'     => $stay_in,
            \'operator\'  => \'IN\', //change to your needs.. IN, NOT IN, AND, EXISTS, NOT EXISTS
        )
    ),
    \'orderby\'           => \'post_date\',
    \'order\'             => \'ASC\',
    \'fields\'            => \'ids\', //only return the post ids, not the whole post-objects
);
$all_posts = new WP_Query( $args );
//search for the current post by its id and look for the previous / next ids
$this_index = array_search( $post->ID, $all_posts->posts );
$prev_id = $all_posts->posts[ $this_index - 1 ];
$next_id = $all_posts->posts[ $this_index + 1 ];
//echo links, if prevoius/next exists
if ( ! empty( $prev_id ) ) :
    echo \'<a rel="prev" href="\' . get_permalink( $prev_id ) . \'">\' . __( \'previous\', \'your_theme_text_domain\' ) . \'</a>\';
endif;

if ( ! empty( $next_id ) ) :
    echo \'<a rel="next" href="\' . get_permalink( $next_id ) . \'">\' . __( \'next\', \'your_theme_text_domain\' ) . \'</a>\';
endif;
wp_reset_postdata();

结束

相关推荐

Show post categories

我正在尝试显示一些面包屑的帖子类别。目前,我有:the_category(\' / \', \'multiple\'); 但出于某种原因,它两次声明了父类别(我只想parent > child):FASHION / DAILY FASHION CANDY / FASHION 它应该是:FASHION / DAILY FASHION CANDY 有人知道它为什么这样做,以及如何改变它吗?