Created
November 15, 2016 09:57
-
-
Save palicko/b662a9303b9e64a001035a479a889e82 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// First, register your taxonomy and set the slug argument of rewrite to shows: | |
register_taxonomy( | |
'show_category', | |
'show', | |
array( | |
'rewrite' => array( 'slug' => 'shows', 'with_front' => false ), | |
// your other args... | |
) | |
); | |
// Next, register your post type and set the slug to shows/%show_category%, and set has_archive argument to shows: | |
register_post_type( | |
'show', | |
array( | |
'rewrite' => array( 'slug' => 'shows/%show_category%', 'with_front' => false ), | |
'has_archive' => 'shows', // THIS IS IMPORTANT, otherwise archive would have wrong link | |
// your other args... | |
) | |
); | |
// Last, add a filter to post_type_link to substitute the show category in individual show permalinks: | |
function wbkn_custom_post_type_permalinks( $post_link, $post ) { | |
if ( is_object( $post ) && $post->post_type == 'show' ) { | |
$terms = wp_get_object_terms( $post->ID, 'show_category' ); | |
if( $terms ) { | |
return str_replace( '%show_category%' , $terms[0]->slug , $post_link ); | |
} | |
} | |
return $post_link; | |
} | |
add_filter( 'post_type_link', 'wbkn_custom_post_type_permalinks', 1, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment