Site icon Hip-Hop Website Design and Development

How to make a proper custom post type link

I make my own type of records and categories to it.

            register_taxonomy( 'instructionscat', [ 'instructions' ], [
                'public'                => true,
                'show_in_nav_menus'     => false,
                'show_ui'               => true,
                'show_tagcloud'         => false,
                'hierarchical'          => true,
                'rewrite'               => array('slug'=>'instructions', 'hierarchical'=>false, 'with_front'=>false, 'feed'=>false ),
                'show_admin_column'     => true, 
                'show_in_rest'          => true
            ] );

            register_post_type( 'instructions', [
                'public'              => true,
                'publicly_queryable'  => true,
                'show_ui'             => true,
                'rest_base'           => '',
                'show_in_menu'        => true,
                'exclude_from_search' => false,
                'capability_type'     => 'post',
                'map_meta_cap'        => true,
                'hierarchical'        => true,
                'rewrite'             => array( 'slug'=>'instructions/%instructionscat%', 'with_front'=>false, 'pages'=>false, 'feeds'=>false, 'feed'=>false ),
                'has_archive'         => 'instructions',
                'query_var'           => true,
                'supports'            => array( 'title', 'editor' ),
                'taxonomies'          => array( 'instructionscat' ),
                'show_in_rest'        => true,
                'menu_icon'           => 'dashicons-book-alt'
            ] );

Next, I do a function with the setting of the link for the records.

add_filter('post_type_link', 'instructioncategory_permalink_structure', 10, 4);
        function instructioncategory_permalink_structure($post_link, $post, $leavename, $sample) {
            if (false !== strpos($post_link, '%instructionscategory%')) {
                $projectscategory_type_term = get_the_terms($post->ID, 'instructionscat');
                if (!empty($projectscategory_type_term))
                    $post_link = str_replace('%instructionscategory%', array_pop($projectscategory_type_term)->slug, $post_link);
                else
                    $post_link = str_replace('%instructionscategory%', 'uncategorized', $post_link);
            }
            return $post_link;
        }

I specify categories and subcategories in the post, but only one category is shown in the postlink, and the parent selections are not shown.

Please help me figure out how to link a post to all categories and subcategories.