Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Publii-recipe-schema

Code snippets for: https://forum.getpublii.com/topic/looking-for-the-best-way-to-include-schema-markup/

Method 1: Custom Head + Post Template + Post Settings

  1. Create a theme override
  2. Duplicate the "head.hbs" partial and name it something new (ie "head-recipes.hbs")
  3. 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}}
  4. 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"
        },
  1. 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>
  1. Save everything, and make sure to restart Publii.
  2. 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.
  3. Generate your recipe's schema and paste it in the text area (without the script tags). I found the following generators that may help:
  4. Preview your post and check the source.

Method 2: Similar, but make use of Publii handlebars and create more custom post settings

  • 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment