K2 Templating: Using K2 Extrafields in a different way
Hmmmm…. Extra Fields! There are many ways to use this feature. So many options!
Of all of these input types, I find the Dropdown option to be an awesome addition to let the client have some fun. Well, or me in this case!
You can use this custom select option to add as many default values as you want. In the case I wanted to add some colors to my personal blog (ryokokawa.com).
Below is an example of how I might go about this:
<?php
$color = "#000";
if($this->item->params->get('catItemExtraFields') && count($this->item->extra_fields)):
foreach ($this->item->extra_fields as $key=>$extraField):
if($extraField->value):
if($extraField->name == 'Color') {
switch($extraField->value) {
case'darkblue':
$color = '#8591ab';
break;
case'blue':
$color = '#68bde9';
break;
case'red':
$color = '#dc270b';
break;
case'orange':
$color = '#F57315';
break;
case'yellow':
$color = '#68bde9';
break;
case'green':
$color = '#b5ca2d';
break;
}
}
endif;
endforeach;
endif;
?>
What I did here is to have the selection/dropdown have options for simple words like “blue” or “red” and translate those in the template to colors. You can define the text/background color or use the $color value as a pre-defined CSS class (hmm… I should have thought of that earlier!).
Another way that you might use this is to change the layout of the page by making the specific layout/css be selectable instead of just color. The extrafields could hold the item template name (ex: category_red.php).
You could build an entirely different layout and have it be switchable from the extra field! For tutorial on switching category_item.php, check out: Switching category_item.php