简体   繁体   中英

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. 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 .

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 !! The Wordpress routing the user directly to 404.php if it exists, Am I missing something? 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/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 :

"has_archive" => true,

I had to add this line in my functions.php for it to work :

flush_rewrite_rules( false );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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