I love K2. I really love it. Often when we have a complex design and want to use extrafields we come across a recurring problem.
We want to use extrafields for acheiving some different layouts but we dont want them all at once.
Currently you have to loop thru the fields and if you want only 3 fields at the top of your template and a different 2 fields somewhere else in your layout its tempting to just loop thru and check if the fieldyou want is there then do your output.
Eg
foreach ($this->item->extra_fields as $key=>$extraField): if $myfield ='what i want' echo $myfieldvalue;
endforeach; ?>
Well I didnt want to do this any more.
So here is an alternative way of calling the extrafields.
Put this at the top of your K2 item template
//setup new array
$exFields = array();
//loop thru extrafields and build collection of key and values
foreach ($this->item->extra_fields as $key=>$extraField): $exFields[$extraField->name] = $extraField->value;
endforeach;
Now you can access each extrafield directly by its name like this without looping
<?php echo $exFields["extraFieldName"]; ?>
So if your extrafield was called 'Address" then you would use
<?php echo $exFields["Address"]; ?>
to output the value.
We want to use extrafields for acheiving some different layouts but we dont want them all at once.
Currently you have to loop thru the fields and if you want only 3 fields at the top of your template and a different 2 fields somewhere else in your layout its tempting to just loop thru and check if the fieldyou want is there then do your output.
Eg
foreach ($this->item->extra_fields as $key=>$extraField): if $myfield ='what i want' echo $myfieldvalue;
endforeach; ?>
Well I didnt want to do this any more.
So here is an alternative way of calling the extrafields.
Put this at the top of your K2 item template
//setup new array
$exFields = array();
//loop thru extrafields and build collection of key and values
foreach ($this->item->extra_fields as $key=>$extraField): $exFields[$extraField->name] = $extraField->value;
endforeach;
Now you can access each extrafield directly by its name like this without looping
<?php echo $exFields["extraFieldName"]; ?>
So if your extrafield was called 'Address" then you would use
<?php echo $exFields["Address"]; ?>
to output the value.