简体   繁体   English

Wordpress 在使用 CPT 和分页时提供自定义 404.php(除第一页之外的所有页面)

[英]Wordpress serving custom 404.php when using CPT and pagination(all pages aside from the first page)

Ok I am lost , I am currently working on a underscore starter Theme with wordpress using PHP.好吧,我迷路了,我目前正在使用 PHP 开发带有 wordpress 的下划线入门主题。 I use some custom post types.我使用一些自定义帖子类型。 So in the homepage I display some custom posts with pagination using the loop因此,在主页中,我使用循环显示了一些带有分页的自定义帖子

  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;

This code is in home.php , also index.php .这段代码在 home.php 中,也在 index.php 中。

Aside from the main page (For the pages http://mywebsite/page/X ,where X is the page number and is >1 ) The website is directly dislaying 404.php , and everything works when I delete the 404.php from the theme !!除了主页(对于页面http://mywebsite/page/X ,其中 X 是页码并且是 >1 )该网站直接显示 404.php ,当我删除 404.php 时一切正常主题 !! The Wordpress routing the user directly to 404.php if it exists, Am I missing something? Wordpress 将用户直接路由到 404.php(如果存在),我是否遗漏了什么? Is this supposed to work this way?这应该以这种方式工作吗? , Link to hierarchy , 链接到层次结构

The Urls that I was supposed to call were not http://mywebsite/page/X but :我应该调用的网址不是http://mywebsite/page/X而是:

 http://mywebsite/my_cpt/page/X

My custom post type archive pages where redirecting to the 404. because I was missing this line in my custom post type registration function :我的自定义帖子类型存档页面重定向到 404。因为我在我的自定义帖子类型注册功能中缺少这一行:

"has_archive" => true,

I had to add this line in my functions.php for it to work :我必须在我的functions.php 中添加这一行才能使其工作:

flush_rewrite_rules( false );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM