douwa6220 2014-10-22 02:34
浏览 57
已采纳

下一个和上一个帖子链接在相同的术语自定义分类

I've created a custom taxonomy => cat-blog in my custom post => blog, cat-blog have 4 terms and each terms have list of post belong to that term

Example of terms:

- City Updates (4 post belong)
- Home Tips (6 post belong)
- Real Estate Guide (8 post belong)
- Real Estate Industry (9 post belong)

and using this query

<?php
     $query = new WP_Query(array('posts_per_page' => 2, 'post_type' => 'blog', 'blog-cat' => get_the_term_list( $post->ID, 'blog-cat' )));
     while ($query->have_posts()) : $query->the_post();
     ?>

   <?php 
// content here
?>

    <?php endwhile; ?>
    <?php wp_reset_query(); ?>
 <?php

 ?>

to display 2 post in same category, AND I just want to put next and prev pagination, so I can navigate the rest of the post, belong to that term.

  • 写回答

1条回答 默认 最新

  • dtuqxb3884 2014-10-22 08:01
    关注

    Don't ever change the main query for a custom query on archive pages and on the home page. The main query already does what you want to do. Trying to run a custom query to try and get the same result is like reinventing the wheel. It also causes problems with pagination

    SOLUTION

    • First, remove your custom query, and return to the main loop. The following is all you need in your taxonomy.php

      if( have_posts() ) {
         while( have_posts() ) {
           the_post();
      
           //REST OF YOUR LOOP
      
         }
      }
      
    • Use pre_get_posts in conjuction with the conditional tags if you need to alter the main query. For instance, if you need 2 posts per page on your taxonomy page, do the following in functions.php

      function so26499451_custom_ppp( $query ) {
          if ( !is_admin() && $query->is_tax() && $query->is_main_query() ) {
              $query->set( 'posts_per_page', '2' );
          }
      }
      add_action( 'pre_get_posts', 'so26499451_custom_ppp' );
      

    You can now paginate as normal without any problems. You will now see two posts from the specific term that you clicked on per page on your taxonomy.php.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路