write BreadcrumbList schema for wordpress single custom post type

I’m on wordpress and for a custom post type ‘tour’ create a single-tour.php and in it I have a structure of breadcrumb like this:

                  <?php
                  global $city_details;
                  $parent_slug = 'tours';
                  $current_post_id = get_the_ID();
                  $city_details = get_related_city_details($parent_slug, $current_post_id, 'both');
                  ?>
                  <!---------------------------- breadcrumb ---------------------------->
                  <nav class="breadcrumb transparent-highlight">
                     <ol class="breadcrumb__list">
                        <li class="breadcrumb__item">
                           <a href="<?php echo site_url(); ?>" class="breadcrumb__link">home</a>
                        </li>
                        <li class="breadcrumb__item">
                           <a href="<?php echo site_url('/tours/'); ?>" class="breadcrumb__link">tour</a>
                        </li>
                        <?php
                        // Output breadcrumb items
                        if (!empty($city_details['breadcrumb'])) {
                           foreach ($city_details['breadcrumb'] as $breadcrumb_item) {
                              echo '<li class="breadcrumb__item">';
                              echo '<a href="' . esc_url($breadcrumb_item['permalink']) . '" class="breadcrumb__link">' . esc_html($breadcrumb_item['title']) . '</a>';
                              echo '</li>';
                           }
                        }
                        ?>
                        <li class="breadcrumb__item">
                           <?php esc_html(the_title()); ?>
                        </li>
                     </ol>
                  </nav>

now I want to add BreadcrumbList schema for each single tour base on this breadcrumb. I have a schema.php that require it in functions.php:

require_once get_template_directory() . '/inc/schema.php';

in schema.php I write this:

add_action('wp_head', 'add_breadcrumb_schema', 20);
function add_breadcrumb_schema()
{
    if (is_singular('tour')) {
        global $city_details;

        // Initialize the breadcrumb schema
        $breadcrumb_schema = [
            '@context' => 'https://schema.org',
            '@type'    => 'BreadcrumbList',
            'itemListElement' => []
        ];

        // Add the homepage breadcrumb
        $breadcrumb_schema['itemListElement'][] = [
            '@type' => 'ListItem',
            'position' => 1,
            'name' => 'home',
            'item' => site_url(),
        ];

        // Add the 'tours' breadcrumb
        $breadcrumb_schema['itemListElement'][] = [
            '@type' => 'ListItem',
            'position' => 2,
            'name' => 'tour',
            'item' => site_url('/tours/'),
        ];

        // Add the city-based breadcrumb items dynamically (items between position 2 and 3)
        if (!empty($city_details['breadcrumb'])) {
            $position = 3; // Start position for the city breadcrumbs
            foreach ($city_details['breadcrumb'] as $breadcrumb_item) {
                $breadcrumb_schema['itemListElement'][] = [
                    '@type' => 'ListItem',
                    'position' => $position,
                    'name' => esc_html($breadcrumb_item['title']),
                    'item' => esc_url($breadcrumb_item['permalink']),
                ];
                $position++;
            }
        }

        // Add the current page (tour)
        $breadcrumb_schema['itemListElement'][] = [
            '@type' => 'ListItem',
            'position' => $position,
            'name' => get_the_title(),
            'item' => get_permalink(),
        ];

        // Output the schema in JSON-LD format within the <head> tag
        echo '<script type="application/ld+json">' . json_encode($breadcrumb_schema) . '</script>';
    }
}

Now the output schema is only have ‘home’ and ‘tour’ as the 1 and 2 position and the third position is current single tour. there should be pages that I add it after position 2 and it shows on breadcrumb but in schema it’s not included. The thing that is I’m sure about it is the declearation of global $city_details; in schema.php did not retrive the value of each single tour but I don’t know how can I fix it. Can anyone tell me how to get this correctly?