2

Im trying to get ExtraFields from K2 Articles. I have assigned my menu to Latest from Users or Categories in order to display all articles from several categories.

It displays fine. But these articles have ExtraFields information that I want to display, but there is no option to display ExtraFields, so I suppose that in order to do this I need to change latest.php page, which is the page used to display all articles from different Categories.

As latest.php doesn't display ExtraFields I'm trying to code it as the following:

<?php  
$extrafields = array();
foreach($this->item->extra_fields as $item)
{    
$extrafields[$item->id] = $item->value;
}   
?>    
<?php if(isset($extrafields[X]) === true):?>
<?php echo $extrafields[X]; ?>
<?php endif; ?>

Though, I'm getting the following error:

Invalid argument supplied for foreach() in ...

I've been searching ways to get ExtraFields, found some solutions but this is the error i get every time.

Any ideas or solutions?

Best regards.

I'm using joomla 2.5.

  • 1
    First: Migrate to Joomla 3.x Second: Get more familiar with PHP. $item->extra_fields seems to be something different than an array. If you need a better answer, you should provide more details. Which 'lastest.php'? Which ExtraFields? – Nils Rückmann Feb 13 '15 at 23:29
  • Hello, thank you for your comment. I don't want to migrate to Joomla 3.x. I want to stay in 2.5 (I know that I should, but not for this website). I provided more details in my question. Can you figure out what I want? – azhpo Feb 16 '15 at 7:38
  • You should ensure that $this->item contains extra fields. Please do a var_dump($this->item); and check for extra fields there. – Dmitry Rekun Feb 16 '15 at 15:08
  • This may not help, but the one time I worked on some K2 stuff, only the item page used $this->item, anything that wasn't the actual item page used $item->. I'm not a programmer though, so it's possible the for each statement will get around that, but I just know in a module we wanted to pull extra fields into did NOT work using $this. – Brian Peat Feb 16 '15 at 17:51
  • Do you know which template you have to modify, and how to make the matching template override? latest.php is part of com_k2, and (at least in the J3.5-version) has a subtemplate called latest_item.php. Create a template override of this one and edit. (and for the template override don't miss to copy the whole folder com_k2/templates since the overriding only works with the files lying in that folder.) – lars k. Feb 18 '15 at 18:37
2

I just did it like this:

This code comes to the file latest_item.php:

<?php if (isset($this->item->extra_fields)) : ?>
  <div class="catItemExtraFields">
    <ul>
    <?php if (is_string($this->item->extra_fields)) {
        $this->item->extra_fields = json_decode($this->item->extra_fields);
      }
      foreach ($this->item->extra_fields as $key=>$extraField) {
        if ($extraField->value != '') {
          echo "<li>".$extraField->name.": ".$extraField->value."</li>";
        }
      }
    ?>
    </ul>
  </div>
<?php endif; ?>

You could do some more checking for existing members and so on.

0

Assuming you want to display data from the extra fields inside the latest.php, then :

Inside the appropriate foreach loop that goes through your content items and prints out each k2 item go as this:

<?php $extra = json_decode($item->extra_fields); ?>

$extra will be an array with the extra fields and their values.

<pre>
<?php print_r($extra); ?>
</pre>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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