0

I changed my permalink structure to /%category/%postname/ and its okay,

My site post permalink is now https://example.com/categoryname/postname/

Now the issue is, I am having child category, It shows on my permalink, which is what I don't want. since I have posts under child category my site permalink is now https://example.com/parentcategory/childcategory/postname/

I Have used this below code to remove all child category from my permalink

function wpse147453_remove_child_categories_from_permalinks( $category ) {
    while ( $category->parent ) {
        $category = get_term( $category->parent, 'category' );
    }

    return $category;
}
add_filter( 'post_link_category', 'wpse147453_remove_child_categories_from_permalinks' );

BUT THE OLD URL IS STILL ACCESSIBLE I.E https://example.com/parentcategory/childcategory/postname/

Is There Any Way To Redirect All The Post Permalink That Include Child Category To The One Without CHILDCATEGORY, I.E https://example.com/parentcategory/childcategory/postname/ => https://example.com/parentcategory/postname/

IT IS CAUSING DUPLICATED POST FOR ME, I DON'T WANT TO USE .HTACCESS BECAUSE IT BREAKS MY SITE PAGINATION.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.