php – WordPress next_post_link / previous_post_link不保持在同一类别

前端之家收集整理的这篇文章主要介绍了php – WordPress next_post_link / previous_post_link不保持在同一类别前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这是我的设置.

single.PHP

  1. <?PHP
  2.  
  3. if ( in_category( 'my-category' ) ) {
  4. include( TEMPLATEPATH.'/single-my-category.PHP' );
  5. }
  6. else {
  7. include( TEMPLATEPATH.'/single-generic.PHP' );
  8. }
  9. ?>

单我-category.PHP

  1. <?PHP
  2.  
  3. if ( have_posts() ) : while (have_posts()) : the_post(); ?>
  4.  
  5. <?PHP echo the_title(); ?>
  6.  
  7. <div class="pagination">
  8. <div class="container">
  9. <div class="row">
  10. <div class="next col-xs-6 col-sm-6 col-md-6 col-lg-6">
  11. <?PHP next_post_link( '%link','<img src="' . get_template_directory_uri() . '/img/cs-left.png" /> PREVIoUS',true ); ?>
  12. </div>
  13.  
  14. <div class="prevIoUs col-xs-6 col-sm-6 col-md-6 col-lg-6">
  15. <?PHP prevIoUs_post_link( '%link','NEXT <img src="' . get_template_directory_uri() . '/img/cs-right.png" />',true ); ?>
  16. </div>
  17. </div>
  18. </div>
  19. </div>
  20.  
  21. <?PHP endwhile; endif; ?>

这就是我所追踪的 – http://codex.wordpress.org/Function_Reference/next_post_link

我不太清楚我在这里做错了什么,因为某些原因,即使函数的in_same_term参数设置为true,prevIoUs_post_link也会将我转到不同类别的帖子.

有任何想法吗?

谢谢.

编辑你的代码块如下.你没有把’.PHP’放在你的single.PHP文件的第5行.上一个/下一个帖子将仅显示您指定的类别的职位,其中if语句为single.PHP(此处为“PHP”).对于以下示例,我创建了一个目录“template-parts”,并在该目录中创建了两个PHP文件(“single-my-category.PHP”和“single-generic.PHP”).

single.PHP

  1. <?PHP
  2. $the_query = new WP_Query( $post );
  3.  
  4. if (in_category('PHP')) {
  5. include( 'template-parts/single-my-category.PHP' );
  6. } else {
  7. include( 'template-parts/single-generic.PHP' );
  8. }
  9. ?>

单我-category.PHP

  1. if ( have_posts() ) : while (have_posts() ) : the_post(); ?>
  2.  
  3. <?PHP the_title(); ?>
  4.  
  5. <div class="pagination">
  6. <div class="container">
  7. <div class="row">
  8. <div class="next col-xs-6 col-sm-6 col-md-6 col-lg-6">
  9. <?PHP next_post_link( '%link',true ); ?>
  10. </div>
  11. </div>
  12. </div>
  13. </div>
  14.  
  15. <?PHP endwhile; endif; ?>

猜你在找的PHP相关文章