Use This…
function chapter_post_type_init() { $labels = array( 'name' => 'Chapters', 'singular_name' => 'Chapter', 'add_new' => 'Add New', 'add_new_item' => 'Add New Chapter', 'edit_item' => 'Edit Chapter', 'new_item' => 'New Chapter', 'all_items' => 'All Chapters', 'view_item' => 'View Chapter', 'search_items' => 'Search Chapters', 'not_found' => 'No chapters found', 'not_found_in_trash' => 'No chapters found in Trash', 'parent_item_colon' => '', 'menu_name' => 'Books' ); $args = array( 'labels' => $labels, 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'show_in_menu' => true, 'taxonomies' => array( 'category', 'post_tag' ), 'query_var' => true, 'rewrite' => array( 'slug' => '/%category%','with_front' => false,'hierarchical' => true), 'capability_type' => 'post', 'has_archive' => true, 'hierarchical' => false, 'menu_position' => 100, 'supports' => array( 'title', 'editor', 'author', 'excerpt') ); register_post_type( 'chapter', $args ); } add_action( 'init', 'chapter_post_type_init' ); add_filter('post_type_link', 'book_permalink_structure', 10, 6); function book_permalink_structure($post_link, $post, $leavename, $sample) { if ( false !== strpos( $post_link, '%category%' ) ) { if( $book_type_term = wp_get_object_terms( $post->ID, 'category' ) ) { if($book_type_term[1]->parent == 0 ){ $post_link = str_replace( '%category%', array_pop( $book_type_term )->slug, $post_link ); }else{ $post_link = str_replace( '%category%', $book_type_term[0]->slug."https://wordpress.stackexchange.com/".$book_type_term[1]->slug, $post_link ); } } } return $post_link; }