Pagination on custom wp_query in WordPress takes to 404 error page

前端 未结 3 1200
春和景丽
春和景丽 2020-12-20 19:10

Im have a loop with wp_query with the following code:

         


        
相关标签:
3条回答
  • 2020-12-20 19:26

    Had same problem with custom post type. I had a query on 'page-template' where the pagination came with 404. I guess the main problem here is the 'slug' of custom post type identical to 'page-template' url. For example if you have a custom post type slug 'portfolio' and a page with the same name, pagination on that page gives a 404. So I just changed 'slug' to 'archives-portfolio' and it helped

    0 讨论(0)
  • 2020-12-20 19:39

    Had a hard time with it too :) Was easier to search when I realized it's wrong calculated post per page number, and here is a magic trick: (to be added to functions.php)

    function my_post_count_queries( $query ) {
      if (!is_admin() && $query->is_main_query()){
        if(is_home()){
           $query->set('posts_per_page', 1);
        }
      }
    }
    add_action( 'pre_get_posts', 'my_post_count_queries' );
    
    0 讨论(0)
  • 2020-12-20 19:47

    you can Change your query

    $wp_query= null; $wp_query = new WP_Query();
    $wp_query->query("showposts=2&paged=$paged");
    

    to

    $wp_query = new WP_Query("showposts=2");
    

    It's shown 2 Post per page and you can see and access your page 2.

    0 讨论(0)
提交回复
热议问题