Download
We're evolving to serve you better! This current forum has transitioned to read-only mode. For new discussions, support, and engagement, we've moved to GitHub Discussions.

Custom code in the head of each post (Alternative Schema, js …)

  • #10246
    Avatar photo[anonymous]

    Although Publii is focused on blogs and news sites, it can also be used for business or any other purpose.

    I noticed that an Article shcema was automatically implemented. This is very positive if the whole website is news and articles but in my case I make websites of all kinds so a good implementation of the schema is key to the success of the web. In this sense, in a very simple way, you can disable the article schema and embed a custom schema.

    The truth is that this is very useful because if we do everything at once we will have a section where embed html and javascript code independently in each article.

    Attached are screenshots and the files to be modified. I used the Simple theme (v.6.2.2)

    Archive: C:\Users\*\Documents\Publii\themes\simple\config.json

        "postConfig": [
                    {
                "name": "customHeadPost",
                "label": "Custom head post",
                "value": "",
                "type": "text",
                "group": "Custom Head post"
                    },
                    {
                "name": "articleSchema",
                "label": "Article Schema",
                "value": 1,
                "type": "select",
                "options": [
                    {
                        "label": "Enabled",
                        "value": 1
                    },
                    {
                        "label": "Disabled",
                        "value": 0
                    }
                    ]
                    },
    .....

    Archive: C:\Users\*\Documents\Publii\themes\simple\partials\head.hbs

    In the head file we locate {{jsonLD}} which is responsible for performing the schema task and replace it with this code:

    {{#if @config.post.articleSchema}}
    {{jsonLD}}
    {{/if}}

    And now in any part before the closing of the <head> we include the following

    {{#if @config.post.customHeadPost}}
    {{{@config.post.customHeadPost}}}
    {{/if}}

     

    This would be all. By default everything remains as usual, but within each post will have added 2 new sections, one to disable the Article Schema for that particular post (you can also configure a global rule in theme settings) and you can also embed custom code in the head of that particular article.

    #10329
    Avatar photo[anonymous]

    This is a great point, thank you for sharing your code!

    I will say, as a suggestion: perhaps there needs to be some kind of schema generator, where people select the type of schema they want in Site settings, and conditional fields pop up where they can fill out additionnal information which Publii tags and helpers can’t auto-populate.

    The nice thing about the current jsonLD helper is that it takes your site and page info to do the work for you.

    #10340
    Avatar photo[anonymous]
    [anonymous] wrote:

    This is a great point, thank you for sharing your code!

    I will say, as a suggestion: perhaps there needs to be some kind of schema generator, where people select the type of schema they want in Site settings, and conditional fields pop up where they can fill out additionnal information which Publii tags and helpers can’t auto-populate.

    The nice thing about the current jsonLD helper is that it takes your site and page info to do the work for you.

    Yes, it is clear that it would be ideal, it crossed my mind and it is something relatively feasible and that it should not take much time. Cheers!