0

I am using custom post types and custom taxonomies for that post type. This snippet currently sets the permalinks for the post type:

'rewrite' => array('slug' => 'myrewritebase'),

I am registering the taxonmy this way:

register_taxonomy(
        'my_taxonomy',
        'my_post_type',

What do I have to set as rewrite for my custom post type?

I tried it this way:

'rewrite' => array('slug' => '%my_taxonomy%'),

This did not work.

Many Thanks for your help!

0

Set your taxonomy slug to taxonomy_name and in your post type set the slug to taxonomy_name/%taxonomy_name_term% and flush rewrite rules (simply by going to permalink settings panel in the admin) And after that WordPress will be able to handle /taxonomy_name/%taxonomy_name_term%/post-name/ URLs.

So all that is left to do is tell WordPress what %taxonomy_name_term% means and for that just add this function to your theme's functions.php file:

add_filter('post_type_link', 'events_permalink_structure', 10, 4);
function events_permalink_structure($post_link, $post, $leavename, $sample)
{
    if ( false !== strpos( $post_link, '%taxonomy_name_term%' ) ) {
        $event_type_term = get_the_terms( $post->ID, 'TAXONOMY_NAME' );
        $post_link = str_replace( '%taxonomy_name_term%', array_pop( $event_type_term )->slug, $post_link );
    }
    return $post_link;
}

Make sure you replace TAXONOMY_NAME with the actual name of the 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.