Skip to content

Instantly share code, notes, and snippets.

@Dimasmagadan
Last active December 20, 2015 23:49
Show Gist options
  • Save Dimasmagadan/6215721 to your computer and use it in GitHub Desktop.
Save Dimasmagadan/6215721 to your computer and use it in GitHub Desktop.
rewrite rules "/cpt-label/category/slug/"
/*
don't forget to flush permalinks
/cpt-label/category/slug/
*/
function os_part_tags() {
add_rewrite_rule("^cpt-label/([^/]+)/page/([^/]+)/?",'index.php?post_type=cpt-label&cat=$matches[1]&paged=$matches[2]','top');
add_rewrite_rule("^cpt-label/([^/]+)/([^/]+)/?",'index.php?cpt-label=$matches[2]','top');
add_rewrite_rule("^cpt-label/?",'index.php?post_type=cpt-label','top');
}
add_action('init','os_part_tags');
function os_adds_link( $post_link, $id = 0 ) {
$post = get_post($id);
if ( is_wp_error($post) || 'cpt-label' != $post->post_type || empty($post->post_name) )
return $post_link;
// Get the model:
$terms = get_the_terms($post->ID, 'category');
if( is_wp_error($terms) || !$terms ) {
$genre = 'uncategorised';
}
else {
$genre_obj = array_pop($terms);
$genre = $genre_obj->slug;
}
return home_url(user_trailingslashit( "cpt-label/$genre/$post->post_name" ));
}
add_filter( 'post_type_link', 'os_adds_link' , 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment