0

I have a post-type Collection with custom-post-type categories.

I want the following url structure: www.baseurl.com/collection/current-category-name/postname

How do I accomplish this?

1

Yes you can use the rewrite parameter when creating your custom post type:

register_post_type( 'example_type',
      array(
          'labels' => array(
              'name' => "Example-Type",
              'singular_name' => "example-type"
          ),
          'public' => true,
          'has_archive' => true,
          'rewrite' => array('slug' => 'the-url-you-want',
      )
  );
}

You will need to reset your permalinks for it to take effect as well.

EDIT:

function custom_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 $post_link;  
}
add_filter( 'post_type_link', 'custom_post_link', 1, 3 );
  • I use 'rewrite' => array('slug' => 'the-url-you-want' to rename the collection, but this doesn't make the url dynamic. This won't put the categorie in the url – waterschaats Jul 6 '17 at 10:27
  • 1
    What you could do is put a replaceable string in your slug - for example %category% ('post-type/%category%')and replace it using the post_type_link filter everytime the URL is returned on the website. I changed my answer above with an example. Hope this helps! – berend Jul 6 '17 at 11:19
0

I had exactly that problem to solve as well and found this post from Diana that solves it:

Permalink rewrite with custom post type and custom taxonomy

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.