structured data met php en JSON-LD
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
$doc = (object)array(
"http://schema.org/name" => "Manu Sporny",
"http://schema.org/url" => (object)array("@id" => "http://manu.sporny.org/"),
"http://schema.org/image" => (object)array("@id" => "http://manu.sporny.org/images/manu.png")
);
$context = (object)array(
"name" => "http://schema.org/name",
"homepage" => (object)array("@id" => "http://schema.org/url", "@type" => "@id"),
"image" => (object)array("@id" => "http://schema.org/image", "@type" => "@id")
);
// compact a document according to a particular context
// see: http://json-ld.org/spec/latest/json-ld/#compacted-document-form
$compacted = jsonld_compact($doc, $context);
echo json_encode($compacted, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
/* Output:
{
"@context": {...},
"image": "http://manu.sporny.org/images/manu.png",
"homepage": "http://manu.sporny.org/",
"name": "Manu Sporny"
}
"http://schema.org/name" => "Manu Sporny",
"http://schema.org/url" => (object)array("@id" => "http://manu.sporny.org/"),
"http://schema.org/image" => (object)array("@id" => "http://manu.sporny.org/images/manu.png")
);
$context = (object)array(
"name" => "http://schema.org/name",
"homepage" => (object)array("@id" => "http://schema.org/url", "@type" => "@id"),
"image" => (object)array("@id" => "http://schema.org/image", "@type" => "@id")
);
// compact a document according to a particular context
// see: http://json-ld.org/spec/latest/json-ld/#compacted-document-form
$compacted = jsonld_compact($doc, $context);
echo json_encode($compacted, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
/* Output:
{
"@context": {...},
"image": "http://manu.sporny.org/images/manu.png",
"homepage": "http://manu.sporny.org/",
"name": "Manu Sporny"
}
De vraag is, ik heb een lijst met leden, moet ik dan in de body een loop aanmaken, die op 1 pagina 50 keer het volgende uitprint?
echo json_encode($compacted, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
Dan is het antwoord nee. En JSON string is niet te combineren met HTML-output.
https://search.google.com/structured-data/testing-tool
Ik lees op verschillende plekken dat hij in de head en body mag staan
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<script type="application/ld+json">
{
"@context" :"http://schema.org",
"@type" :"BlogPosting",
"editor" :"<?=$uname?>",
"publisher": {
"@type" : "person",
"name" :"<?=$uname?>"
},
"genre" :"About <?=$r['reg1']?>",
"keywords" :"<?=$r['reg1']?>",
"datePublished" :"<?=$r['datum']?>",
"dateCreated" :"<?=$r['datum']?>",
"headline" :"<?=$r['title']?>",
"description" :"<?=$short?>",
"articleBody" : "<?=$long?>",
"image": {
"@type" : "ImageObject",
"url" : "<?=HTML_ROOT?>blogs/<?=$r['reg1Url']?>/images/<?=$r['seo_title']?>_50.jpg",
"height" : "50",
"width" : "50"
},
"url" :"<?=HTML_ROOT?>blog/<?=$r['reg1Url']?>/<?=$r['seo_title']?>",
"author": {
"@type": "Person",
"name": "<?=$uname?>"
}
}
</script>
{
"@context" :"http://schema.org",
"@type" :"BlogPosting",
"editor" :"<?=$uname?>",
"publisher": {
"@type" : "person",
"name" :"<?=$uname?>"
},
"genre" :"About <?=$r['reg1']?>",
"keywords" :"<?=$r['reg1']?>",
"datePublished" :"<?=$r['datum']?>",
"dateCreated" :"<?=$r['datum']?>",
"headline" :"<?=$r['title']?>",
"description" :"<?=$short?>",
"articleBody" : "<?=$long?>",
"image": {
"@type" : "ImageObject",
"url" : "<?=HTML_ROOT?>blogs/<?=$r['reg1Url']?>/images/<?=$r['seo_title']?>_50.jpg",
"height" : "50",
"width" : "50"
},
"url" :"<?=HTML_ROOT?>blog/<?=$r['reg1Url']?>/<?=$r['seo_title']?>",
"author": {
"@type": "Person",
"name": "<?=$uname?>"
}
}
</script>
Wat laat je browser zelf zien?
alle php variabelen zijn gevuld.
ik krijg alleen 1 foutmelding in die code:
Het kenmerk publisher.itemtype heeft een ongeldige waarde.
Edit:
Het is dus JSON LD (Linked Data), dat maakt het dus weer anders dan het gewone JSON.
Gewijzigd op 10/08/2017 09:20:59 door - Ariën -
Ik ben niet geheimzinnig, het is erg normaal dat mensen niet hun domeinnamen etc tonen om niet hier indexeert te worden, en de output kan je toch gewoon zien, alleen de HTML_ROOT etc zijn domeinennamen
Inmiddels zie ik ook dat het JSON+LD is i.p.v. het gewone JSON. Dat verandert de zaak, en dat kan wel in je HTML-document.
Gewijzigd op 10/08/2017 09:31:29 door - Ariën -