WordPress自定义文章类型重写后分页/first/second/page/2出现404错误求助
Let's break down why you're hitting that 404 and fix it step by step. The core issue here is a rewrite rule priority conflict: your WordPress page and custom post type (CPT) share the same base URL (/first/second), so when you try to access the paginated archive, WordPress is trying to route the request to the page's pagination (which doesn't exist) instead of your CPT's archive pagination.
Here's how to resolve this:
1. Adjust Your CPT Registration to Prioritize Archive & Pagination Rules
When registering your CPT, tweak the rewrite arguments to explicitly tell WordPress to recognize permalink and page routes for this post type. This helps WordPress prioritize CPT-related requests over the static page.
Update your CPT registration code like this:
function register_my_custom_post_type() { $args = array( 'public' => true, 'label' => 'Your CPT Name', 'rewrite' => array( 'slug' => 'first/second', 'with_front' => false, 'ep_mask' => EP_PERMALINK | EP_PAGES // Flag to handle single posts and pagination ), // Add your other CPT settings here (supports, taxonomies, etc.) ); register_post_type( 'your_cpt_slug', $args ); } add_action( 'init', 'register_my_custom_post_type' );
2. Manually Add a High-Priority Pagination Rewrite Rule
If the above adjustment doesn't solve the issue, we can directly add a rewrite rule that specifically targets the paginated archive URL and routes it to your CPT. This rule will take precedence over the static page's rule.
Add this code to your theme's functions.php file or a custom plugin:
function add_cpt_pagination_rewrite_rule() { add_rewrite_rule( '^first/second/page/(\d+)/?$', 'index.php?post_type=your_cpt_slug&paged=$matches[1]', 'top' // Set this to "top" to ensure it's checked first ); } add_action( 'init', 'add_cpt_pagination_rewrite_rule' );
Make sure to replace your_cpt_slug with the actual slug you used when registering your CPT.
3. Flush Your Rewrite Rules (Critical Step!)
After making these code changes, you need to refresh WordPress's rewrite rule cache. Here's how:
- Go to your WordPress admin dashboard → Settings → Permalinks
- You don't need to change the setting—just click the Save Changes button. This will flush the rewrite rules and apply your new ones.
4. Verify Your Page Template Handles CPT Pagination Correctly
Since you're using a static page to display the CPT archive, ensure your page template is using the correct query to pull CPT posts and generate valid pagination links.
Example of how to structure your page template:
<?php /* Template Name: CPT Archive Page */ get_header(); // Override the main query to fetch your CPT posts with pagination global $wp_query; $paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1; $args = array( 'post_type' => 'your_cpt_slug', 'paged' => $paged, 'posts_per_page' => 10 // Adjust to your desired posts per page ); $wp_query = new WP_Query( $args ); // Loop through CPT posts if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post(); // Display your post content here the_title( '<h2>', '</h2>' ); the_excerpt(); endwhile; // Generate pagination links echo paginate_links( array( 'total' => $wp_query->max_num_pages, 'current' => $paged ) ); wp_reset_postdata(); // Reset the query to avoid conflicts else : echo '<p>No posts found.</p>'; endif; get_footer(); ?>
Quick Troubleshooting Tip
If you're still seeing the 404, install the Rewrite Rules Inspector plugin to view all active rewrite rules. Check if your pagination rule is listed and appears above the rule for your static page. If it's not, double-check that you set the 'top' priority in the add_rewrite_rule function.
内容的提问来源于stack exchange,提问作者Chris

