如何在 Wordpress 中动态生成面包屑架构(所有页面、子页面、自定义 post 类型和分类法)

How to Generate Breadcrumb Schema Dynamically in Wordpress (all pages, child pages, custom post type and taxonomy)

<script type="application/ld+json">
{
  "@context": "https://schema.org",
  "@type": "BreadcrumbList",
  "itemListElement": [{
    "@type": "ListItem",
    "position": 1,
    "name": "Home",
    "item": "https://example.com/"
  },{
    "@type": "ListItem",
    "position": 2,
    "name": "<?php the_title(); ?>",
    "item": "<?php the_permalink(); ?>"
  }
  ]
}

我将此代码用于动态面包屑架构,但它仅适用于第一级页面和默认 post ,我需要处理所有自定义 post 类型和自定义分类法。你能帮帮我吗?

试试这个脚本

<script>
    var bread = {
        "@@context": "https://www.schema.org",
        "@@type": "BreadcrumbList",
        "itemListElement": []
    }
    var exist = false;
    $('.breadcrumb li').each(function (index) {
        var item = {}
        var href = $(this).find("a").attr('href');
        if (href) item["@@id"] = "@Repository.Settings["WebSiteAddress"]" + href // OR location.protocol+"//"+location.host+href;
        else item["@@id"] = "@Repository.Settings["WebSiteAddress"]" + window.location.pathname
        item["name"] = $.trim($(this).text());

        bread.itemListElement.push({
            "@@type": "ListItem",
            "position": index + 1,
            item
        })
        exist = true;
    });
    if(exist){
        var jsonStrb = JSON.stringify(bread);
   var s2 = document.createElement("script");
   s2.type = "application/ld+json";
   s2.id = "BreadcrumbJson";
   $("body").append(s2);
    $('#BreadcrumbJson').append(jsonStrb);
    }        </script>

<?php

// This function will take $_SERVER['REQUEST_URI'] and build a breadcrumb based on the user's current path
function breadcrumbs($separator = ' &raquo; ', $home = 'Home') {
    // This gets the REQUEST_URI (/path/to/file.php), splits the string (using '/') into an array, and then filters out any empty values
    $path = array_filter(explode('/', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)));

    // This will build our "base URL" ... Also accounts for HTTPS :)
    $base = ($_SERVER['HTTPS'] ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . '/';

    // Initialize a temporary array with our breadcrumbs. (starting with our home page, which I'm assuming will be the base URL)
    $breadcrumbs = Array("<a href=\"$base\">$home</a>");

    // Find out the index for the last value in our path array
    $last = end(array_keys($path));

    // Build the rest of the breadcrumbs
    foreach ($path AS $x => $crumb) {
        // Our "title" is the text that will be displayed (strip out .php and turn '_' into a space)
        $title = ucwords(str_replace(Array('.php', '_'), Array('', ' '), $crumb));

        // If we are not on the last index, then display an <a> tag
        if ($x != $last)
            $breadcrumbs[] = "<a href=\"$base$crumb\">$title</a>";
        // Otherwise, just display the title (minus)
        else
            $breadcrumbs[] = $title;
    }

    // Build our temporary array (pieces of bread) into one big string :)
    return implode($separator, $breadcrumbs);
}

?>