Adding custom code to only certain posts
-
October 13, 2021 at 12:07 pm #6438[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!
October 16, 2021 at 3:22 am #6447[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/.
October 19, 2021 at 6:16 am #6459[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.
October 19, 2021 at 8:39 am #6462[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.
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.
October 19, 2021 at 8:49 am #6467Bob[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:
- 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" }
- 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.
October 19, 2021 at 8:51 am #6468BobThanks, shaun2k for the help, looks like we responded at the same time 🙂
October 24, 2021 at 5:55 pm #6491[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.
October 25, 2021 at 8:06 am #6492[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" },
October 26, 2021 at 7:23 am #6495BobI recommended this tool https://jsonlint.com/ to validate your JSON file.
- 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)