【问题标题】:Wordpress serving custom 404.php when using CPT and pagination(all pages aside from the first page)Wordpress 在使用 CPT 和分页时提供自定义 404.php(除第一页外的所有页面)
【发布时间】:2020-06-30 12:40:44
【问题描述】:

好吧,我迷路了,我目前正在使用 PHP 开发带有 wordpress 的下划线启动主题。 我使用一些自定义帖子类型。因此,在主页中,我使用循环显示了一些带有分页的自定义帖子

  global $wp_query;
 $wp_query = new WP_Query( array(
    'post_type' => 'my_cpt',
    'posts_per_page' => 8,
    'paged' => $paged
  )
); 
if ( $wp_query->have_posts() ) : 
 while ( $wp_query->have_posts() ) :    $wp_query->the_post(); //display the post .. which I did
 endwhile;

//Pagination starts here
 $total_pages = $wp_query->max_num_pages;

 if ($total_pages > 1){

     $current_page = max(1, get_query_var('paged'));

     echo paginate_links(array(
         'base' => get_pagenum_link(1) . '%_%',
         'format' => '/page/%#%',
         'current' => $current_page,
         'total' => $total_pages,
         'prev_text'    => __('« prev'),
         'next_text'    => __('next »'),
     ));
 } //Pagination ends here
endif;

这段代码在 home.php 中,也在 index.php 中。

除了主页(对于页面http://mywebsite/page/X,其中X是页码并且>1)该网站直接显示404.php,当我从主题中删除404.php时一切正常! ! Wordpress 将用户直接路由到 404.php(如果存在),我是否遗漏了什么? 这应该以这种方式工作吗? , Link to hierarchy

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    我应该调用的网址不是http://mywebsite/page/X,而是:

     http://mywebsite/my_cpt/page/X
    

    我的自定义帖子类型存档页面重定向到 404。因为我在自定义帖子类型注册功能中缺少这一行:

    "has_archive" => true,
    

    我必须在我的 functions.php 中添加这一行才能让它工作:

    flush_rewrite_rules( false );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-12
      • 1970-01-01
      • 2011-07-08
      • 2021-03-02
      • 2011-08-05
      • 2012-09-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多