Wordpress next_post_link/previous_post_link不属于同一类别

And*_*hew 9 php wordpress

这是我的设置.

single.php中

<?php

    if ( in_category( 'my-category' ) ) {
        include( TEMPLATEPATH.'/single-my-category.php' );
    }
    else {
        include( TEMPLATEPATH.'/single-generic.php' );
    }
?>
Run Code Online (Sandbox Code Playgroud)

单我-category.php

<?php

if ( have_posts() ) : while (have_posts()) : the_post(); ?>

<?php echo the_title(); ?>

<div class="pagination">
    <div class="container">
        <div class="row">
            <div class="next col-xs-6 col-sm-6 col-md-6 col-lg-6">
                <?php next_post_link( '%link', '<img src="' . get_template_directory_uri() . '/img/cs-left.png" /> PREVIOUS', true ); ?>
            </div>

            <div class="previous col-xs-6 col-sm-6 col-md-6 col-lg-6">
                <?php previous_post_link( '%link', 'NEXT <img src="' . get_template_directory_uri() . '/img/cs-right.png" />', true ); ?>
            </div>
        </div>
    </div>
</div>

<?php endwhile; endif; ?>
Run Code Online (Sandbox Code Playgroud)

这就是我所遵循的 - http://codex.wordpress.org/Function_Reference/next_post_link

由于某种原因,即使函数的in_same_term参数设置为true,previous_post_link也会将我带到不同类别的帖子,我不太确定我在这里做错了什么.

有任何想法吗?

谢谢.

Muk*_*hal -2

您可以将此代码添加到您的模板文件中吗?

<ul class="pager">
  <?php if ( get_previous_post() != null ) : ?>
    <li class="previous">
      <span class="nav-previous">
        <?php 
          $singular_nav_previous_text = apply_filters( 'tc_singular_nav_previous_text', _x( '&larr;' , 'Previous post link' , 'customizr' ) );
          previous_post_link( '%link' , '<span class="meta-nav">' . $singular_nav_previous_text . '</span> %title' ); 
        ?>
      </span>
    </li>
  <?php endif; ?>
  <?php if ( get_next_post() != null ) : ?>
    <li class="next">
      <span class="nav-next">
        <?php
          $singular_nav_next_text = apply_filters( 'tc_singular_nav_next_text', _x( '&rarr;' , 'Next post link' , 'customizr' ) );
          next_post_link( '%link' , '%title <span class="meta-nav">' . $singular_nav_next_text . '</span>' ); 
          ?>
      </span>
    </li>
  <?php endif; ?>
</ul>
Run Code Online (Sandbox Code Playgroud)

现在在function.php中添加以下代码

add_filter('tc_previous_single_post_link_args', 'navigate_in_same_taxonomy');
add_filter('tc_next_single_post_link_args', 'navigate_in_same_taxonomy');
function navigate_in_same_taxonomy( $args ){
  $args['in_same_term'] = true;
  return $args;
}
Run Code Online (Sandbox Code Playgroud)

如果您需要下一个/上一个链接的更多选项,请检查此链接