Rewrite slug for Custom Post Type?
-
Hello,
I have registered a custom post type called “books”, which supports categories and tags. In order to show the category in the permalink a rewrite array is created in my CPT:rewrite' => array('slug' => 'books/%category%
.Now, in order to get the correct category, the following rewrite code is written in my functions.php:
//Get category add_filter('post_type_link', 'custom_permalink_structure', 10, 4); function custom_permalink_structure($post_link, $post, $leavename, $sample) { if ( false !== strpos( $post_link, '%category%' ) ) { $glossary_letter = get_the_terms( $post->ID, 'category' ); // adding check and reset if ( !$glossary_letter ) { $glossary_letter = array(); } // continue as you were $post_link = str_replace( '%category%', array_pop( $glossary_letter )->slug, $post_link ); } return $post_link; } //**End
This all works correct, but three things go not as desired:
1: When i post a cpt only in a subcategory, i.e. genre/adventure, then the URL is generated as follows: domain.com/books/adventure/post-name. However the default WP structure would generate an URL as follows (which is desired): domain.com/books/genre/adventure/post-name.2: When i post a CPT in a subcategory and in the concerning parent category, i.e. parent category “genre” and subcategory “adventure”, the URL is generated as follows: domain.com/books/adventure/post-name. Whereas the default WP structure (which is desired) would generate an URL as follows: domain.com/books/genre/post-name
3: When i go to the archive page of a CPT, the URL looks as follows: domain.com/books/%category%. This is of course logical, since the slug is built that way. However, is there a way to filter the %category% tag when no %category% is defined?
I hope that someone can help me out with this. Help is greatly appreciated!!
Thanks, Robbert
- The topic ‘Rewrite slug for Custom Post Type?’ is closed to new replies.