我想为我用循环显示的自定义POST类型档案创建分页

时间:2017-11-28 作者:amin foroutan

我已经创建了一个家。php文件,其中我显示了我的自定义帖子类型“带有WP查询的项目”。我已经使用“paginate\\u links()”将分页添加到我的自定义帖子类型中,并显示了列表,但当您单击它们时,会显示“找不到错误”。下面是我正在使用的循环代码:

   $args = array(
     \'post_type\' => \'projects\',
     \'posts_per_page\' => 3,
     \'paged\'=> $paged,
     \'order_by\' => \'date\',
                );
      $projects = new WP_Query( $args );

      while( $projects -> have_posts() ): $projects -> the_post();
  ///////// the content of the post types
       endwhile;
这是我用于分页链接的代码:

    $big = 999999999; // need an unlikely integer
  echo paginate_links( array(
        \'base\' => str_replace( $big, \'%#%\', esc_url( get_pagenum_link( $big ))),
        \'format\' => \'?paged=%#%\',
        \'current\' => max( 1, get_query_var(\'paged\') ),
        \'total\' => $projects->max_num_pages
                ) );

1 个回复
SO网友:Narek Zakarian

您可以这样尝试:在$args之前添加以下内容

        if( get_query_var( \'paged\' ) ):
            $my_page = get_query_var( \'paged\' );
        else:
            if( get_query_var( \'page\' ) ):
                $my_page = get_query_var( \'page\' );
        else:
                $my_page = 1;
        endif;
        $paged = $my_page;
用此替换分页代码(请确保在endwhile):

$total = $projects->found_posts;
            $page = isset( $_GET[\'page\'] ) ? abs( (int) $_GET[\'page\'] ) : 1;
            $format = \'page/%#%/\';
            $current_page = max(1, $paged);
            $big = 999999999;
            echo paginate_links( array(
                \'base\' => str_replace( $big, \'%#%\', esc_url( get_pagenum_link( $big ) ) ),
                \'format\' => \'?page=%#%\',
                \'end_size\'           => 1,
                \'mid_size\'           => 2,
                \'prev_next\'          => True,
                \'prev_text\'          => \'prev\',
                \'next_text\'          => \'next\',
                \'type\'               => \'plain\',
                \'add_args\'           => False,
                \'add_fragment\'       => \'\',
                \'before_page_number\' => \'\',
                \'after_page_number\'  => \'\',
                \'total\' => ceil($total / $posts_per_page),
                \'current\' => $current_page,
            ));

结束

相关推荐

Custom taxonomy pagination

我正在为自定义分类法存档页创建自定义编号分页。因此,我在显示分页、显示正确的链接等方面没有问题。但是,当我单击第2页上方的链接时(例如第3页,如:my-website.com/page/3/?my_category=some-term我得到404-页面不存在。该类别包含60多篇帖子,应该有8页,但只有第一页和第二页正确显示my-website.com/?my_category=some-term和my-website.com/page/2/?my_category=some-term但以上所有的都是404