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.

Adding custom code to only certain posts

  • #6438
    Avatar photo[anonymous]

    Hi there, and apologies in advance – I am not a developer and have only very basic  HTML/CSS knowledge, so please be gentle. 🙂

    I would like to add some code to only certain blog entries (posts), not to all of them. The code in question is from the VG Wort, the German Collection Management Organisation for authors. So far I am using WordPress, and of course there’s a plugin for adding the VG Wort’s “counting pixel” and unique ID number to your articles with a minimum number of words (so that they can be counted and at the end of the year you get a few € in royalties, yay!).

    I found the article in your documentation on how to add custom code to posts, but there seems to be an all or nothing approach. Or I am too stupid to understand the concept. How can I turn this off or on for the posts matching the criteria?

    The code would look something like this (example from the VG Wort documentation).

    It should appear between the <body> tags.

    What exactly do I need to do (and where) to set this up, so that I can manually add that line to posts?

    I am using the “Mercury” theme.

    Thank you in advance!

    #6447
    Avatar photo[anonymous]

    Hi,

    I’m still a novice user but from what I’ve learnt, one way is to create a custom post template which you put the custom code. Then when you create those special blog posts, use that custom post template instead of the default post template.

    @candidexmedia has a rather detailed run-through of the steps in his comment to the post:

    https://forum.getpublii.com/topic/post-template-adress-templates/.

    #6459
    Avatar photo[anonymous]

    Thank you and I’m sorry for not responding sooner, I somehow didn’t get notified that there was a reply. I’ll read carefully through that custom template solution and see if this works for me. Thanks!

    Edit: I should probably note that the code in question is not always the exact same, it changes the number for each new blog post, so that the post’s hits can be measured individually. So just having one custom template always featuring the exact same code wouldn’t work, and neither ,of course, creating one new custom template for every new blog post that needs the code. But I need the functionality to manually insert the new tracking number into the line of code for each new blog post.

    #6462
    Avatar photo[anonymous]

    Since a different code is required for each new blog post, I see 2 methods to go about inserting the code.

    Method 1 – Easy way without editing template files

    Directly insert the code at the end of your blog post with HTML.  For example, if you are creating the post with Block editor, add a new HTML block by clicking on the <> option. Then insert the line of code.

    1
    2

    Method 2 – Elaborate way with custom post option

    Add a custom post config option to add text (reference — https://getpublii.com/dev/post-config-options/#textbr).

    In the theme config.json

    "postConfig": [
        {
            "name": "insertVGWortCode",
            "label": "Insert VG Wort Code",
            "value": "",
            "type": "text"        
        },

    In the post.hbs file, add just before the closing {{/post}}

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

    When creating the blog post, click on the “gear” icon,  then scroll down and click on “Other options”.  You will see the “Insert VG Wort Code” box where you can paste the line of code. The line of code will be inserted into the webpage when the website is rendered.

    If the box is left blank, nothing extra will be added to the rendered webpage.

    3

    #6467
    Avatar photoBob
    [anonymous] wrote:

    I need the functionality to manually insert the new tracking number into the line of code for each new blog post.

    You can realize your idea with the Post config options .

    In short:

    1. add input or textarea to the post options (which will be available in the right sidebar under “Other options”), for example, add the following code to the “postConfig” section in the config.json file (‎⁨Documents ▸ ⁨Publii⁩ ▸ ⁨sites⁩ ▸ ⁨YOUR_SITE ▸ ⁨input⁩ ▸ ⁨themes⁩ ▸ ⁨YOUR_THEME)
      "postConfig": [
              {
                  "name": "displayMyCode",
                  "label": "Custom Code",
                  "value": "",
                  "type": "textarea"        
              }
    2. open the footer.hbs file (‎⁨Documents ▸ ⁨Publii⁩ ▸ ⁨sites⁩ ▸ ⁨YOUR_SITE ▸ ⁨input⁩ ▸ ⁨themes⁩ ▸ ⁨YOUR_THEME ▸ partials) and before the closing body tag add as follow:
      {{{@config.post.displayMyCode}}}

    That’s it.

    #6468
    Avatar photoBob

    Thanks, shaun2k for the help, looks like we responded at the same time 🙂

    #6491
    Avatar photo[anonymous]

    Thank you, I guess I’m just too stupid for it. I’ve added the bits to the the config.json and the footer.hbs, but I don’t see any option in the post, under “other options” it says “Not available in your theme” on the greed out dropdown menu. And when I click preview I get an error warning:

    Rendering process failed
    An error (1010) occurred during parsing config.json file of the theme.
    
    Please check your theme config.json file as it seems to be corrupted.
    
    #6492
    Avatar photo[anonymous]

    Hi,

    If you are using Bob’s example code for config.json, do remember to add a comma at the end of that block of code like so:

    "postConfig": [
            {
                "name": "displayMyCode",
                "label": "Custom Code",
                "value": "",
                "type": "textarea"        
            },
    #6495
    Avatar photoBob

    I recommended this tool https://jsonlint.com/ to validate your JSON file.