Skip to content

Instantly share code, notes, and snippets.

@derpyherp
Created December 15, 2022 14:20
Show Gist options
  • Save derpyherp/11aeaab43d8ef9c2c2c7d2f00fc3b9a5 to your computer and use it in GitHub Desktop.
Save derpyherp/11aeaab43d8ef9c2c2c7d2f00fc3b9a5 to your computer and use it in GitHub Desktop.

Here's how to create custom fields for posts in Publii that function similarly to those in WordPress.

Adding a custom field

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".

Displaying custom fields

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}}
@derpyherp
Copy link
Author

mycustomfield (1)

postview (1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment