自定义分类法的自定义文章类型日期归档问题
Hey there! Let’s work through that date archive issue for your Resources custom post type (CPT) and Resource Types taxonomy. I’ve helped folks fix similar setup quirks before, so here’s a straightforward, actionable approach:
Step 1: Double-Check Your CPT & Taxonomy Registration
First, make sure your CPT and taxonomy are registered with the right parameters to support archives and rewrite rules. Update their registration code in your theme’s functions.php (or a custom plugin) like this:
Register the Resources CPT:
function register_resources_cpt() { $args = array( 'public' => true, 'has_archive' => true, 'rewrite' => array( 'slug' => 'resources', 'with_front' => false ), 'supports' => array( 'title', 'editor', 'date' ), // Critical for date-based archives // Add any other existing parameters here ); register_post_type( 'resources', $args ); } add_action( 'init', 'register_resources_cpt' );
Register the Resource Types Taxonomy:
function register_resource_types_taxonomy() { $args = array( 'hierarchical' => true, 'rewrite' => array( 'slug' => 'resources', 'with_front' => false, 'hierarchical' => true ), // Add any other existing parameters here ); register_taxonomy( 'resource_types', 'resources', $args ); } add_action( 'init', 'register_resource_types_taxonomy' );
This ensures your taxonomy URLs (/resources/media/, /resources/blog/) map correctly to the Resources CPT.
Step 2: Add Custom Rewrite Rules
WordPress doesn’t automatically recognize date archives for taxonomy-specific CPTs, so we need to add rewrite rules to tell it how to handle URLs like /resources/blog/2024/05/. Drop this code into your functions.php:
function add_resource_tax_date_rewrite_rules() { // Rule for year/month/day archives add_rewrite_rule( '^resources/([^/]+)/([0-9]{4})/([0-9]{2})/([0-9]{2})/?$', 'index.php?resource_types=$matches[1]&post_type=resources&year=$matches[2]&monthnum=$matches[3]&day=$matches[4]', 'top' ); // Rule for year/month archives add_rewrite_rule( '^resources/([^/]+)/([0-9]{4})/([0-9]{2})/?$', 'index.php?resource_types=$matches[1]&post_type=resources&year=$matches[2]&monthnum=$matches[3]', 'top' ); // Rule for annual archives add_rewrite_rule( '^resources/([^/]+)/([0-9]{4})/?$', 'index.php?resource_types=$matches[1]&post_type=resources&year=$matches[2]', 'top' ); } add_action( 'init', 'add_resource_tax_date_rewrite_rules' );
Step 3: Flush Rewrite Rules
After adding the rules, you need to refresh WordPress’s rewrite cache. The easiest way is to:
- Go to Settings → Permalinks in your WP dashboard
- Make no changes—just click Save Changes
This will flush the rules and make your new URLs work.
Step 4: Customize Your Archive Template
To display the date-specific content correctly, tweak your taxonomy archive template (likely taxonomy-resource-types.php in your theme) with this snippet to add date context and display filtered posts:
<?php get_header(); // Grab current taxonomy and date parameters $current_taxonomy = get_queried_object(); $archive_year = get_query_var( 'year' ); $archive_month = get_query_var( 'monthnum' ); $archive_day = get_query_var( 'day' ); // Build a dynamic header echo '<h1 class="archive-title">'; echo esc_html( $current_taxonomy->name ); if ( $archive_year ) { echo ' - ' . esc_html( $archive_year ); if ( $archive_month ) { echo '/' . esc_html( str_pad( $archive_month, 2, '0', STR_PAD_LEFT ) ); if ( $archive_day ) { echo '/' . esc_html( str_pad( $archive_day, 2, '0', STR_PAD_LEFT ) ); } } } echo '</h1>'; // Loop through matching posts if ( have_posts() ) : while ( have_posts() ) : the_post(); // Use your existing content template or add custom markup here get_template_part( 'template-parts/content', 'resources' ); endwhile; // Add pagination if needed the_posts_navigation(); else : echo '<p>No resources found for this date range.</p>'; endif; get_footer(); ?>
Quick Notes to Avoid Headaches
- Make sure all your Resources CPT posts have valid publish dates—date archives rely entirely on this field.
- If you’re using any
pre_get_postsfilters elsewhere, double-check they don’t override the taxonomy/date query (add a condition likeif ( is_tax( 'resource_types' ) && is_date() ) return;to skip modifying this specific query).
内容的提问来源于stack exchange,提问作者Jose

