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’ => ‘color’
- ‘Name of custom field’ => ‘name_of_custom_field’
- ‘Max speed (km/h)’ => ‘max_speed_(km/h)’
Entering data into custom fields
The fields defined here will appear as input boxes in the "Expert" edit form of all posts in the current blog.
They will also appear in the "Properties" section of the in-skin 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:
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 custom_fields() are:
- 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:
If the given $field_index doesn’t exist or it is not set for the Item, $value will be false.
Getting custom field definitions for a blog
Note: the following is not guaranteed to work in all upcoming versions on b2evolution.
You can get all defined custom fields from the current Blog if you call:
If you want to dig into the internal storage you can do:
Created by • Last edit by on Oct 13, 2015
No feedback yet
Form is loading...