23 Aug 2015

Output specific K2 extra fields

Outputing specific K2 extra fields is dependent on what php document you intend to display the extra fields in. If you're using item.php, or the category-item.php files, the following should work. In the top of the document, add this code:

<!-- Call to prepare extra fields -->
$extrafields = array();
foreach($this->item->extra_fields as $item)
$extrafields[$item->id] = $item->value;
Then, wherever you'd like to call the value of the filled in field, use this:

<?php if($extrafields[ID_NUMBER_OF_FIELD]!=''):?> <!-- if filled in, then call data -->
<?php echo $extrafields[ID_NUMBER_OF_FIELD];?> <!-- actual data call -->
<?php endif; ?>
tag.php works a little differently, as do the K2 Modules.

for tag.php you don't need the extra code in the head, the following will call your field data:

<?php $extrafields = json_decode($item->extra_fields);?>
<?php foreach($extrafields as $key=>$value): ?>
<?php if($extrafields[$key]->id == 'ID_NUMBER_OF_FIELD'&&$extrafields[$key]->value!=''): ?>
<?php echo $extrafields[$key]->value; ?>,
<?php endif; ?>
<?php endforeach; ?>
Again, with the module_k2_content, you don't need the top data call. Get your field data this way:

<?php echo $item->extraFields->ALIAS_OF_FIELD->value ;?>


For menu links that use the K2 Latest Items layout, call the extra fields within the template override using the following:

<?php $extrafields = json_decode($this->item->extra_fields);?>
<?php foreach($extrafields as $key=>$value): ?>
<?php if($extrafields[$key]->id == '1'&&$extrafields[$key]->value!=''): ?>
<?php echo $extrafields[$key]->value; ?>
<?php endif; ?>
<?php endforeach; ?>