Goal
Display JSON similar to this from links that have the same category minus the current page.
"relatedLink": [ "https://example.com/category-slug/page-title.html", "https://example.com/category-slug/page-title.html", "https://example.com/category-slug/page-title.html" ],
Code
{% capture results %}[
{% for category in page.categories %}
{% for post in site.categories[category] %}
{% if post.url != page.url %}
{{ post.url | relative_url | prepend: site.url | replace:" ", "," | jsonify }}{% endif %}{% if forloop.last %}]{% else %}{% endif %}
{% endfor %}{% endfor %}{% endcapture %}
"relatedLink": {{ results }},
Results Error in JSON-LD
"relatedLink": [ "https://example.com/category-slug/page-title.html" "https://example.com/category-slug/page-title.html" "https://example.com/category-slug/page-title.html" ],
jsonify
did not comma separate the values in the array.
CodePudding user response:
I think this is expected behavior. You're calling jsonify
on post.url
. jsonify
converts Hashs or Arrays to JSON, not strings.
The best way to get what you want is probably this:
{% capture results %}[
{% for category in page.categories %}
{% for post in site.categories[category] %}
{% if post.url != page.url %}
"{{ post.url | relative_url | prepend: site.url }}"{% unless forloop.last %},{% endunless %}
{% endif %}
{% endfor %}
{% endfor %}]
{% endcapture %}
"relatedLink": {{ results }}