Closed tibbis closed 1 week ago
This might be because of how the key
feature is implemented in your loop.
Have you tried using the append
method to ensure each listItem
is correctly added to the itemListElement
array without overwriting the previous items?
yep what @amabilee said is likely what is happening here.
Also check out some samples here that add a list of things:
https://nystudio107.com/blog/annotated-json-ld-structured-data-examples
This might be because of how the
key
feature is implemented in your loop. Have you tried using theappend
method to ensure eachlistItem
is correctly added to theitemListElement
array without overwriting the previous items?
I tried{% do itemListJsonLd.append(listItem) %}
but "append" method does not exist in this context.
I managed to get it working by adding a static key to the ItemList and then:
{% set updatedItemList = itemListJsonLd.getAttributes().itemListElement | merge([listItem]) %}
{% do itemListJsonLd.setAttributes({
"itemListElement": updatedItemList
}, false) %}
Question
I'm struggling to create a ItemList with products inside a itemListElement array JsonLd object using the key feature in a for loop. Only the last item is correctly added to the itemListElement, the rest as separate ItemLists. What am I doing wrong?
I could of course create an array by merging them and then adding it to seomatic.jsonld but prefer to avoid doing that.
Resulting in:
Additional context
Craft CMS Pro v5.4.9 + Seomatic plugin.