Code snippets for: https://forum.getpublii.com/topic/looking-for-the-best-way-to-include-schema-markup/
- Create a theme override
- Duplicate the "head.hbs" partial and name it something new (ie "head-recipes.hbs")
- Create a custom post template that you'll use for recipes (ie "post-Recipes.hbs"). In this custom template, swap out the old
{{> head}}
for the new head partial{{> head-recipes}}
- Create a new version of "config.json" in your theme override. Edit this file, and add a custom post setting for this new template via the Theme Settings API (textarea type):
"postConfig": [
{
"name": "recipeSchema",
"label": "Recipe Schema ld+json script code",
"note": "Help get your recipes on Google",
"value": "",
"type": "textarea",
"rows": 50,
"cols": 100,
"postTemplates": "recipes"
},
- Edit the new head partial ("head-recipes.hbs") and add this inside the head (perhaps below the
{{jsonLD}}
handlebar):
<script type="application/ld+json">{{{ @config.post.recipeSchema }}}</script>
- Save everything, and make sure to restart Publii.
- Open Publii and create a new post/recipe. Open the post settings, and expand "Other options". Select your recipes template. The Recipe Schema text area should appear.
- Generate your recipe's schema and paste it in the text area (without the script tags). I found the following generators that may help:
- Preview your post and check the source.
- Using the previously described method to create custom post settings and dynamically using them in a post template, you could add a boilerplate for the parts of the script that stay the same, and create custom fields for the property values.
- Some of the values may not even require a custom post setting since Publii may have a a handlebar tags for it already. Consult Post tags and Featured image tags.
- For example, you'll likely find handlebars tags for the author name, image URLs, post name, publish date
Found these tips helpful? Consider supporting my work so that I can keep going. 😊
Thanks!