Same slug for Custom Taxonomy archive and CPT archive – Rewrite rule not working
-
I’m using a CPT (wg_seo_chat) together with a Custom taxonomy (wg_chat_category). Additional rewrite rules are in place to inject the Taxonomy into the URL:
-
CPT archive: domain/hilfe-chat
Taxonomy archive: domain/hilfe-chat/allgemein
Item page: domain/hilfe-chat/general/itemI’d like to keep the same slug
/hilfe-chat/
for both CPT and Taxonomy archives, which seems to generate a conflict (404 for Taxonomy archives).CPT
$rewrite = array( 'slug' => 'hilfe-chat', 'has_archive' => 'hilfe-chat',
Custom taxonomy
$args = array( 'rewrite' => array('slug' => 'hilfe-chat') );
Rewrite rules for custom taxonomy
/** Rewrite rule to handle third level slug */ function chat_cpt_generating_rule($wp_rewrite) { $rules = array(); $terms = get_terms( array( 'taxonomy' => 'wg_chat_category', 'hide_empty' => false, ) ); $post_type = 'wg_seo_chat'; foreach ($terms as $term) { $rules['hilfe-chat/' . $term->slug . '/([^/]*)$'] = 'index.php?post_type=' . $post_type. '&wg_seo_chat=$matches[1]&name=$matches[1]'; } // merge with global rules $wp_rewrite->rules = $rules + $wp_rewrite->rules; } add_filter('generate_rewrite_rules', 'chat_cpt_generating_rule'); /** Inject the chat category into the CPT Link */ function change_link( $permalink, $post ) { if( $post->post_type == 'wg_seo_chat' ) { $chat_terms = get_the_terms( $post, 'wg_chat_category' ); $term_slug = ''; if( ! empty( $chat_terms ) ) { foreach ( $chat_terms as $term ) { // The featured chat will have another category which is the main one if( $term->slug == 'featured' ) { continue; } $term_slug = $term->slug; break; } } $permalink = get_home_url() ."/hilfe-chat/" . $term_slug . '/' . $post->post_name; } return $permalink; } add_filter('post_type_link',"change_link",10,2);
Any thoughts on how to resolve this conflict are much appreciated.
Thanks, Jan
The page I need help with: [log in to see the link]
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘Same slug for Custom Taxonomy archive and CPT archive – Rewrite rule not working’ is closed to new replies.