- b2evolution CMS User Manual
- Developer Reference
- Website Skins/Themes
- Displaying Custom Fields in a Template
Displaying Custom Fields in a Template
This page explains how you can use Custom Fields with PHP code in order to display them exactly the way you want inside of your templates. This is especially useful if you want to build custom templates, either generally or for specific item types.
Note: there are other – potentially easier – ways to display custom fields:
- With a widget: Item Custom Fields Widget
- From within the post contents, by using Custom Fields Short Tags
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:
<?php $Item->custom_fields(); ?>
If you want to only display these fields in single mode (when a single post is displayed), use this instead:
<?php
if( $disp == 'single' )
{ // Display custom fields
$Item->custom_fields();
}
?>
Possible additional params to custom_fields() are
- ‘before’,
- ‘after’
- ‘field_format’ ( example:
<tr><th>$title$:</th><td>$value$</td></tr>
). - ‘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:
<?php $Item->custom( array( 'field' => $field_index ) ); ?>
Possible params to $Item->custom()
are:
- common params:
- ‘before’
- ‘after’
- ‘field’ (mandatory)
- string custom field params:
- ‘format’
- numeric custom field params:
- ‘decimals’
- ‘dec_point’
- ‘thousands_sep’
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:
<?php $value = $Item->get_custom_field_value( $field_index, $restrict_type ); ?>
$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),false
fo all