最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

custom post types - Pagination not working for archive

programmeradmin0浏览0评论

The query works for the default blog and pages but not for custom post archives. Did I miss something?

$paged_bottom = (get_query_var('paged')) ? absint(get_query_var('paged')) : 1;
              $args_pagination_bottom  = array(
                  'format'    => '?paged=%#%',
                  'current'   => intval($paged_bottom),
                  'total'     => intval($GLOBALS['wp_query']->max_num_pages),
                  'mid_size'  => 2,
                  'prev_text' => '<i class="fa fa-arrow-left"></i>',
                  'next_text' => '<i class="fa fa-arrow-right"></i>',
              );

              echo paginate_links($args_pagination_bottom);

i tried this

public function __construct()
              {


                  add_action( 'pre_get_posts', array($this, 'ggowl_archive_posts_per_page'),20,1 );

              }

              public function ggowl_archive_posts_per_page( $query ) {
                  // var_dump($query);

                  if ( $query->is_archive('product') || $query->is_category() ) {
                      set_query_var('posts_per_page', 1);
                  }
              }

this works outside the class. How can i make it work in class?

The query works for the default blog and pages but not for custom post archives. Did I miss something?

$paged_bottom = (get_query_var('paged')) ? absint(get_query_var('paged')) : 1;
              $args_pagination_bottom  = array(
                  'format'    => '?paged=%#%',
                  'current'   => intval($paged_bottom),
                  'total'     => intval($GLOBALS['wp_query']->max_num_pages),
                  'mid_size'  => 2,
                  'prev_text' => '<i class="fa fa-arrow-left"></i>',
                  'next_text' => '<i class="fa fa-arrow-right"></i>',
              );

              echo paginate_links($args_pagination_bottom);

i tried this

public function __construct()
              {


                  add_action( 'pre_get_posts', array($this, 'ggowl_archive_posts_per_page'),20,1 );

              }

              public function ggowl_archive_posts_per_page( $query ) {
                  // var_dump($query);

                  if ( $query->is_archive('product') || $query->is_category() ) {
                      set_query_var('posts_per_page', 1);
                  }
              }

this works outside the class. How can i make it work in class?

Share Improve this question edited Sep 13, 2019 at 19:58 asked Sep 13, 2019 at 19:06 user145078user145078 2
  • What's the code for your actual loop? – Jacob Peattie Commented Sep 14, 2019 at 2:25
  • @JacobPeattie it's having the same common used structure, just with query loop. I think it should not be used in class as multiple instances of the same code runs every time the class is called. So I created a sperate options page for controlling this setting and now it's working good. Thank you – user145078 Commented Sep 14, 2019 at 16:13
Add a comment  | 

1 Answer 1

Reset to default -1

You might need a different var ... set_query_var('posts_per_archive_page', 1);

posts_per_archive_page (int) – number of posts to show per page – on archive pages only. Over-rides posts_per_page and showposts on pages where is_archive() or is_search() would be true.

发布评论

评论列表(0)

  1. 暂无评论