Removing parent slug from URL on Custom Post Type

// Register Custom Post Type ...... $args = array( 'hierarchical' => true, 'has_archive' => true, 'rewrite' => array('slug' => '/','with_front' => false), ); //functions.php <?php function wpse_101072_flatten_hierarchies( $post_link, $post ) { if ( 'your_custom_post_type' != $post->post_type ) return $post_link; $uri = ''; foreach ( $post->ancestors as $parent ) { $uri = get_post( $parent )->post_name . "/" . $uri; } return str_replace( $uri, '', $post_link ); } add_filter( 'post_type_link', 'wpse_101072_flatten_hierarchies', 10, 2 ); ?>
This part of code remove the parent slug of our Custom Post Type.

From : yourdomain.com/CPT-SLUG/post-name
To : yourdomain.com/post-name


Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.