Saturday, December 10, 2022
HomeWordPress Developmentcustomization - Identical slug for Customized Taxonomy archive and CPT archive -...

customization – Identical slug for Customized Taxonomy archive and CPT archive – Rewrite rule not working


I’m utilizing a CPT (wg_seo_chat) along with a Customized taxonomy (wg_chat_category). Extra rewrite guidelines are in place to inject the Taxonomy into the URL:

I might prefer to maintain the identical slug /hilfe-chat/ for each CPT and Taxonomy archives, which appears to generate a battle (404 for Taxonomy archives).

CPT

    $rewrite = array(
        'slug'                  => 'hilfe-chat',
        'has_archive'           => 'hilfe-chat',

Customized taxonomy

        $args = array(
            'rewrite'           => array('slug' => 'hilfe-chat')
        );

Rewrite guidelines for customized taxonomy

/** Rewrite rule to deal with third stage slug */

        operate chat_cpt_generating_rule($wp_rewrite) {
            $guidelines = array();
            $phrases = get_terms( array(
                'taxonomy' => 'wg_chat_category',
                'hide_empty' => false,
            ) );
           
            $post_type="wg_seo_chat";
        
            foreach ($phrases as $time period) {    
                        
                $guidelines['hilfe-chat/' . $term->slug . '/([^/]*)$'] = 'index.php?post_type=" . $post_type. "&wg_seo_chat=$matches[1]&title=$matches[1]';
                                
            }
        
            // merge with world guidelines
            $wp_rewrite->guidelines = $guidelines + $wp_rewrite->guidelines;
        }
        add_filter('generate_rewrite_rules', 'chat_cpt_generating_rule');

        /** Inject the chat class into the CPT Hyperlink */

        operate change_link( $permalink, $put up ) {
    
            if( $post->post_type == 'wg_seo_chat' ) {
                $chat_terms = get_the_terms( $put up, 'wg_chat_category' );
                $term_slug = '';
                if( ! empty( $chat_terms ) ) {
                    foreach ( $chat_terms as $time period ) {
        
                        // The featured chat can have one other class which is the primary one
                        if( $term->slug == 'featured' ) {
                            proceed;
                        }
        
                        $term_slug = $term->slug;
                        break;
                    }
                }
                $permalink = get_home_url() ."/hilfe-chat/" . $term_slug . "https://wordpress.stackexchange.com/" . $post->post_name;
            }
            return $permalink;
        
        }
        add_filter('post_type_link',"change_link",10,2);

Any ideas on resolve this battle are a lot appreciated.

Thanks,
Jan

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments