I’m building a recipe site that uses custom post types for recipes.
So I searched for including the custom post type categories in the url and found a solution below by Milo. It rewrites the url and adds the category. The only problem is; the archive page of the entire custom post type is now located at /recept/%category%/
Solution Milo:
Add category base to url in custom post type/taxonomy
Code being used:
'rewrite' => array('slug' => 'recept/%category%','with_front' => false),
function wpa_course_post_link( $post_link, $id = 0 ){
$post = get_post($id);
if ( is_object( $post ) ){
$terms = wp_get_object_terms( $post->ID, 'category' );
if( $terms ){
return str_replace( '%category%' , $terms[0]->slug , $post_link );
//return str_replace( 'recept/' , 'recept/'.$terms[0]->slug , $post_link );
}
}
return $post_link;
I tried to delete the %category%
from the slug rewrite and insert it through the function. But that breaks down all my category pages and the archive still won’t work.