You will find an example of a post with custom fields when you install b2evolution:
Defining custom fields
You can define custom fields in the Post Type Form.
Custom fields can contain numbers (floating point numbers with double precision) or text strings (varchars). Although you could store numbers as text strings, storing them in number fields will make a difference when you try to sort posts based on the numbers stored here (numbers will sort as 1,2,3,9,10,11 whereas text strings would sort as 1,10,11,2,3,9)
Define as many custom fields of each type as you want and give them a name each. Each custom field has a field index which can be generated from the field name. The field index is the trimmed lowercase value of the field name where the white spaces are converted to one single underline character. Field indexes must be unique in a blog ( this way field names are also unique ).
Sample field names and corresponding field indexes:
- ‘Color’ =>
- ‘Name of custom field’ =>
- ‘Max speed (km/h)’ =>
Entering data into custom fields
The fields defined here will appear as form fields in the "Additional fields" section of the Item edit form.
Displaying the data entered in custom fields
Displaying all fields for a post
The fields that contain values for a post will be displayed at the end of that post. This of course requires that the skin includes the proper code to do that. Here is the proper code in case you need to display all custom fields of an Item:
If you want to only display these fields in single mode (when a single post is displayed), use this instead:
Possible additional params to custom_fields() are
- ‘field_format’ ( example:
- ‘fields’ (Empty string to display ALL fields, OR fields names separated by comma to display only requested fields in order what you want)
Displaying a specific field
If you want to display a specific custom field of the item you must know the index of the custom field ( the field index can be generated from the field name as it was described above ).
Here is the proper code in case you need to display a specific custom field of an Item:
Possible params to
- common params:
- ‘field’ (mandatory)
- string custom field params:
- numeric custom field params:
If the given $field_index doesn’t exist or it is not set for the Item, then nothing will be displayed.
Extracting data for advanced uses
Getting a value for further processing
If you don’t want to display, just want to get a value of an Item custom field you can do:
$field_index: if it doesn’t exist or it is not set for the Item, $value will be false.
$restrict_type: Restrict field by type (double, varchar, html, text, url),
Created by • Last edit by on May 05, 2017
No feedback yet
Form is loading...