Here's how to create custom fields for posts in Publii that function similarly to those in WordPress.
First, create an override folder for your theme. For example, if your website uses the Simple theme...
\Publii\sites\my-website\input\themes\simple
...the override folder would be:
\Publii\sites\my-website\input\themes\simple-override
Copy the config.json
file from the simple
to the simple-override
folder. Open it in your favorite code editor and find the postConfig
array. Add a new element for your custom field (don't forget to add a comma after the previous element):
{
"name": "myCustomField",
"label": "My custom field",
"value": "",
"type": "text"
}
Relaunch Publii and open the post editor for your site. On the right side, click Other options
and scroll down to the bottom. You will see a new text field called "My custom field".
Create more override files as needed - for example, index.hbs for the main index of your website, or post.hbs for the single post view.
In the single post view, you can display the contents of your custom field using {{@config.post.myCustomField}}
.
Elsewhere, such as on the front page, you can display the contents of your custom field using {{@postViewConfig.myCustomField}}
.
It's always a good idea to check if the field exists first:
{{#if postViewConfig.myCustomField}}
<p class="align-center">{{postViewConfig.myCustomField}}</p>
{{/if}}