Permalink for CPT without taxonomy
-
I have a CPT with custom taxonomies. I have a page or 2 that are not assigned a custom taxonomy. How can I get those permalinks to work?
examples:
//This works base_url/post-type/section/has-section-taxonomy/ // How can I get this to work? base_url/post-type/has-no-section-taxonomy/
Register CPT excerpt:
'rewrite' => ['slug' => 'post-type/%sections%'] register_post_type('post-type', $args);
Register Taxonomy excerpt:
$post_types = ['post-type']; register_taxonomy('sections', $post_types, $args);
function permalinks( $post_link, $post ){ if ( is_object( $post ) && $post->post_type == 'post-type' ){ $terms = wp_get_object_terms( $post->ID, 'sections' ); if( $terms ){ return str_replace( '%sections%' , $terms[0]->slug , $post_link ); } } return $post_link; } add_filter( 'post_type_link', 'permalinks', 1, 2 );
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘Permalink for CPT without taxonomy’ is closed to new replies.