8

I'm building a site that uses K2 extra fields, but I'm customizing the output so I'm not just pulling the entire extra field block.

Normally something like this works for single entry fields:

<?php if($this->item->extraFields->streetaddressone->value != ''): ?>
 <span class="itemExtraFieldsValue Address1"><?php echo $this->item->extraFields->streetaddressone->value ?></span>
  <?php endif; ?>

But I have a field that is a multi-select list and I want to pull the entire array into a line on my item page.

I'm a bit of a hack, so I just got stuck on this one.

3 Answers 3

9

It depends on what .php document you're in - if you're in 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 --> 
<?php  
$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 does the K2 Modules.

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 ;?>

Hope that helps.

3
  • I am in item.php and that totally worked. Thank you!
    – Brian Peat
    Commented Apr 23, 2014 at 16:14
  • No problem!!! I spent many a hour figuring this out, glad it could help someone else.
    – Faye
    Commented Apr 23, 2014 at 16:15
  • FYI, that code was originally written for me by Jordan. He's on here too, he knows awesome stuff and makes things work for me when I look like I'm about to cry. joomla.stackexchange.com/users/75/jordan-ramstad
    – Faye
    Commented Apr 25, 2014 at 18:21
2

@reactionfaye 's reply above is correct. For reference, this has been part of K2 since Nov 2012 :)

Originally introduced in this blog post for the release of K2 v2.6.2 (http://getk2.org/blog/1068-k2-v262-now-available):

So how would you directly output individual extra fields in your K2 overrides? Simple. Just do something like this (e.g. in item.php) to get the extra field name:

$this->item->extraFields->EXTRAFIELDALIASHERE->name

To get the extra field value you would simply do this:

$this->item->extraFields->EXTRAFIELDALIASHERE->value

Now replace EXTRAFIELDALIASHERE with the actual alias of the extra field you wish to output.

-3

This can be done with a language override now, Avoiding overwriting your hacks when you upgrade K2. Look for the constant K2_DATE_FORMAT_LC

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.