平台开发中,欢迎参与测试。你可以在 QQ群:1046115671 中与我们交流,或是直接在社区发帖

自定义字段

LitePress has the ability to allow post authors to assign custom fields to a post. This arbitrary extra information is known as metadata. This metadata can include bits of information such as:

  • Mood:Happy
  • Currently Reading:Cinderella
  • Listening To: Rock Around the Clock
  • Weather: Hot and humid

With some extra coding, it is possible to achieve more complex actions, such as using the metadata to store an expiration date for a post.

Metadata is handled with key/value pairs. The key is the name of the metadata element. The value is the information that will appear in the metadata list on each individual post that the information is associated with.

Keys can be used more than once per post. For example, if you were reading two different books (perhaps a technical book at work and a fiction at home), you could create a “reading” key and use it twice on the same post, once for each book.

Here is an example of what this information might look like on your post:

Currently Reading:Calvin and Hobbes
Today's Mood:Jolly and Happy

用法

Based upon our example above, let’s add two custom fields, one called “Currently Reading” and the other “Today’s Mood”. Please follow the below steps to add this information to a post using Custom Fields.

  1. Custom Fields options on the Post & Page edit screens are hidden by default if they have not been used before. Using the Block Editor, click the three dots button at the top of the right sidebar and visit Options to enable it. (If you are using the Classic editor, check the screen options).
  2. After you have written your post, scroll down to the area titled 自定义字段.
  3. To create a new Custom Field called “Currently Reading”, enter the text “Currently Reading” (without the quotes) in the text entry field titled Name.
  4. The newly created Key (“Currently Reading”) should now be assigned a Value, which in our case is the name of the book currently being read, “Calvin and Hobbes”. Type “Calvin and Hobbes” in the Value field, again without the quotes.
  5. ClickAdd Custom Field button to save this custom information for that post.
自定义字段

To add your “Today’s Mood”, repeat the process and add “Today’s Mood” to the key and a description of your mood in the value text boxes and click Add Custom Field to save this information with the post.

On your next post, you can add a new book and mood to your metadata. In the 自定义字段 section, the Key will now feature a pull-down list with the previously entered Custom Fields. Choose “Currently Reading” and then enter the new book you are reading in the value. Click Add Custom Field and then repeat the process to add “Today’s Mood”.

You only need to create a new “KEY” once, after which you can assign a value to that key for every post, if you so desire. You can also assign more than one Value to a key, for a post. This will come in handy for people who read more than one book at a time.

Displaying Custom Fields

To display the Custom Fields for each post, you have to use a template tag that customizes your theme. For more details about it, please refer to:

You may install Plugin that manages custom fields.

  • Meta Box plugin – Plugin that allows you to create custom meta boxes and custom fields.
  • Piklist – Plugin that allows you to create custom meta boxes and fields everywhere in LitePress.
  • Advanced Custom Fields – Plugin that allows you to create complex fields and layouts using a user friendly interface.