2015/09/19

JSON LD BreadcrumbList

規格參考
https://developers.google.com/structured-data/breadcrumbs

語法驗證
http://json-ld.org/playground/index.html

最終驗證
https://developers.google.com/structured-data/testing-tool/

實作範例 PHP
json_ld = array();
$json_ld["@context"] = "http://schema.org";
$json_ld["@type"] = "BreadcrumbList";
$json_ld["itemListElement"] = [];
$position = 1;

foreach ($forum_parents_name_url_mapping as $f_name => $f_url) {
$breadcrumb_item = array();
$breadcrumb_item["@type"] = "ListItem";
$breadcrumb_item["position"] = $position;
$breadcrumb_item_detail = array();
$f_url = preg_replace('/\?.*/', '', $f_url);
$breadcrumb_item_detail["@id"] = $f_url;
$breadcrumb_item_detail["name"] = $f_name;
$breadcrumb_item["item"] = $breadcrumb_item_detail;

$json_ld["itemListElement"][] = $breadcrumb_item;
$position = $position + 1;
}

json_encode($json_ld)

No comments:

Post a Comment