// Breadcrumb Schema (Page-Specific, JSON Safe)
add_action('wp_head', function () {

  if (is_admin()) return;

  global $post;
  if (!$post) return;

  $items = [];
  $position = 1;

  // Home
  $items[] = [
    "@type" => "ListItem",
    "position" => $position++,
    "name" => "Home",
    "item" => home_url('/')
  ];

  // Parent pages (if any)
  if ($post->post_parent) {
    $parents = array_reverse(get_post_ancestors($post));
    foreach ($parents as $parent_id) {
      $items[] = [
        "@type" => "ListItem",
        "position" => $position++,
        "name" => wp_strip_all_tags(get_the_title($parent_id)),
        "item" => get_permalink($parent_id)
      ];
    }
  }

  // Current page
  $items[] = [
    "@type" => "ListItem",
    "position" => $position++,
    "name" => wp_strip_all_tags(get_the_title($post)),
    "item" => get_permalink($post)
  ];

  $data = [
    "@context" => "https://schema.org",
    "@type" => "BreadcrumbList",
    "itemListElement" => $items
  ];
  ?>
<script type="application/ld+json">
<?php echo wp_json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); ?>
</script>
  <?php
});
