Sign in

Adding custom code to only certain posts

  • This topic has 8 replies, 3 voices, and was last updated 8 months, 1 week ago by .
Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #6438
    K

    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
    shaun2k

    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
    K

    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
    shaun2k

    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
    Bob

    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.

    --
    Do you appreciate the support you've received today? If so, consider donating to the Publii team by clicking here; we'll be sure to use your donation to make Publii even better!

    #6468
    Bob

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

    --
    Do you appreciate the support you've received today? If so, consider donating to the Publii team by clicking here; we'll be sure to use your donation to make Publii even better!

    #6491
    K

    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
    shaun2k

    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
    Bob

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

    --
    Do you appreciate the support you've received today? If so, consider donating to the Publii team by clicking here; we'll be sure to use your donation to make Publii even better!

Viewing 9 posts - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.