title: Joomla K2 show extra fields on generic.php
body: <p>We are developing a website based on k2. For this we make use of the extrafields to give an article an specific css class.</p>
  
  <p>But when we use the archive extension this class is not applied and we get an error.</p>
  
  <p>This is the error we get </p>
  
  <blockquote>
    <p>Warning: Invalid argument supplied for foreach() in
    /home/recall/domains/domain.com/public_html/templates/mytemplate/html/com_k2/generic.php
    on line 19</p>
  </blockquote>
  
  <p>This is the code</p>
  
  <pre><code>&lt;!-- START: Call to prepare extra fields --&gt;
  &lt;?php  
  //convertArray to use ids as key
  $extrafields = array();
  foreach($this-&gt;item-&gt;extra_fields as $item)
  {    
  $extrafields[$item-&gt;id] = $item-&gt;value;
  }   
  ?&gt;    
  &lt;!-- END: Call to prepare extra fields --&gt;
  
  &lt;!-- Start K2 Generic (search/date) Layout --&gt;
  &lt;div id="k2Container" class="genericView&lt;?php echo $this-&gt;escape($this-&gt;params-&gt;get('page-id')); ?&gt; nieuwsarchief"&gt;
  
          &lt;?php if($this-&gt;params-&gt;get('genericFeedIcon',1)): ?&gt;
          &lt;!-- RSS feed icon --&gt;
          &lt;ul class="k2SendReceiveNews"&gt;
                  &lt;li class="legende_bestuur"&gt;&lt;span&gt;Bestuur&lt;/span&gt;&lt;/li&gt;
                  &lt;li class="legende_regio"&gt;&lt;span&gt;Regio&lt;/span&gt;&lt;/li&gt;
                  &lt;li class="legende_school"&gt;&lt;span&gt;School&lt;/span&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;a href="#"&gt;Nieuws melden&lt;/a&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;a href="#"&gt;Nieuws via e-mail ontvangen&lt;/a&gt;&lt;/li&gt;
                  &lt;li class="rssfeed"&gt;&lt;a href="&lt;?php echo $this-&gt;feed; ?&gt;" title="&lt;?php echo JText::_('K2_SUBSCRIBE_TO_RSS'); ?&gt;"&gt;
                          &lt;span&gt;&lt;?php echo JText::_('K2_SUBSCRIBE_TO_RSS'); ?&gt;&lt;/span&gt;
                  &lt;/a&gt;&lt;/li&gt;
                  &lt;div class="clr"&gt;&lt;/div&gt;
          &lt;/ul&gt;
          &lt;?php endif; ?&gt;
  
          &lt;?php if($this-&gt;params-&gt;get('show_page_title')): ?&gt;
          &lt;!-- Page title --&gt;
          &lt;div class="componentheading&lt;?php echo $this-&gt;params-&gt;get('pageclass_sfx')?&gt;"&gt;
                  &lt;?php echo $this-&gt;escape($this-&gt;params-&gt;get('page_title')); ?&gt;
          &lt;/div&gt;
          &lt;?php endif; ?&gt;
  
          &lt;?php if(count($this-&gt;items)): ?&gt;
          &lt;div&gt;
                  &lt;?php foreach($this-&gt;items as $item): ?&gt;
  
                  &lt;!-- Start K2 Item Layout --&gt;
                  &lt;div class="CatItem-&lt;?php echo $extraFlds['Nieuwscategorie']; ?&gt;"&gt;
                  &lt;?php
  echo $item-&gt;extra_fields[11]-&gt;value;
  ?&gt;
  
                          &lt;div class="catItemContent catItemHeader"&gt;
  
                            &lt;?php if($this-&gt;params-&gt;get('genericItemTitle')): ?&gt;
                            &lt;!-- Item title --&gt;
                            &lt;h3 class="catItemTitle"&gt;
                                  &lt;?php if ($this-&gt;params-&gt;get('genericItemTitleLinked')): ?&gt;
                                          &lt;a href="&lt;?php echo $item-&gt;link; ?&gt;"&gt;
                                          &lt;?php echo $item-&gt;title; ?&gt;
                                  &lt;/a&gt;
                                  &lt;?php else: ?&gt;
                                  &lt;?php echo $item-&gt;title; ?&gt;
                                  &lt;?php endif; ?&gt;
                            &lt;/h3&gt;
                            &lt;?php endif; ?&gt;
                            &lt;?php if($this-&gt;params-&gt;get('genericItemDateCreated')): ?&gt;
                                  &lt;!-- Date created --&gt;
                                  &lt;span class="genericItemDateCreated"&gt;
                                          &lt;?php echo JHTML::_('date', $item-&gt;created , JText::_('DATE_FORMAT_LC10')); ?&gt;
                                  &lt;/span&gt;
                                  &lt;?php endif; ?&gt;
                    &lt;/div&gt;
  
                  &lt;/div&gt;
                  &lt;!-- End K2 Item Layout --&gt;
  
                  &lt;?php endforeach; ?&gt;
          &lt;/div&gt;
  
          &lt;!-- Pagination --&gt;
          &lt;?php if($this-&gt;pagination-&gt;getPagesLinks()): ?&gt;
          &lt;div class="k2Pagination"&gt;
                  &lt;?php echo $this-&gt;pagination-&gt;getPagesLinks(); ?&gt;
                  &lt;div class="clr"&gt;&lt;/div&gt;
                  &lt;?php echo $this-&gt;pagination-&gt;getPagesCounter(); ?&gt;
          &lt;/div&gt;
          &lt;?php endif; ?&gt;
  
          &lt;?php endif; ?&gt;
  
  &lt;/div&gt;
  &lt;!-- End K2 Generic (search/date) Layout --&gt;
  </code></pre>
  
  <hr>
  
  <p>I've found the solution myself.</p>
  
  <p>This is the final code:</p>
  
  <pre><code>&lt;?php
  /**
   * @version     $Id: generic.php 1618 2012-09-21 11:23:08Z lefteris.kavadas $
   * @package     K2
   * @author      JoomlaWorks http://www.joomlaworks.net
   * @copyright   Copyright (c) 2006 - 2012 JoomlaWorks Ltd. All rights reserved.
   * @license     GNU/GPL license: http://www.gnu.org/copyleft/gpl.html
   */
  
  // no direct access
  defined('_JEXEC') or die;
  
  ?&gt;
  
  &lt;!-- Start K2 Generic (search/date) Layout --&gt;
  &lt;div id="k2Container" class="genericView&lt;?php echo $this-&gt;escape($this-&gt;params-&gt;get('page-id')); ?&gt; nieuwsarchief"&gt;
  
      &lt;?php if($this-&gt;params-&gt;get('genericFeedIcon',1)): ?&gt;
      &lt;!-- RSS feed icon --&gt;
      &lt;ul class="k2SendReceiveNews"&gt;
          &lt;li class="legende_bestuur"&gt;&lt;span&gt;Bestuur&lt;/span&gt;&lt;/li&gt;
          &lt;li class="legende_regio"&gt;&lt;span&gt;Regio&lt;/span&gt;&lt;/li&gt;
          &lt;li class="legende_school"&gt;&lt;span&gt;School&lt;/span&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href="#"&gt;Nieuws melden&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href="#"&gt;Nieuws via e-mail ontvangen&lt;/a&gt;&lt;/li&gt;
          &lt;li class="rssfeed"&gt;&lt;a href="&lt;?php echo $this-&gt;feed; ?&gt;" title="&lt;?php echo JText::_('K2_SUBSCRIBE_TO_RSS'); ?&gt;"&gt;
              &lt;span&gt;&lt;?php echo JText::_('K2_SUBSCRIBE_TO_RSS'); ?&gt;&lt;/span&gt;
          &lt;/a&gt;&lt;/li&gt;
          &lt;div class="clr"&gt;&lt;/div&gt;
      &lt;/ul&gt;
      &lt;?php endif; ?&gt;
  
      &lt;?php if($this-&gt;params-&gt;get('show_page_title')): ?&gt;
      &lt;!-- Page title --&gt;
      &lt;div class="componentheading&lt;?php echo $this-&gt;params-&gt;get('pageclass_sfx')?&gt;"&gt;
          &lt;?php echo $this-&gt;escape($this-&gt;params-&gt;get('page_title')); ?&gt;
      &lt;/div&gt;
      &lt;?php endif; ?&gt;
  
      &lt;?php if(count($this-&gt;items)): ?&gt;
      &lt;div&gt;
  
          &lt;?php foreach($this-&gt;items as $item): ?&gt;
  
          &lt;!-- START: Call to prepare extra fields --&gt;
          &lt;?php
          //convertArray to use ids as key
          $extrafields = array();
          foreach($item-&gt;extra_fields as $extraFlds)
          {    
          $extrafields[$extraFlds-&gt;id] = $extraFlds-&gt;value;
          }   
          ?&gt;    
          &lt;!-- END: Call to prepare extra fields --&gt;
  
          &lt;!-- Start K2 Item Layout --&gt;
          &lt;div class="CatItem-&lt;?php echo $extrafields[11];?&gt;"&gt;
  
              &lt;div class="catItemContent catItemHeader"&gt;
  
                &lt;?php if($this-&gt;params-&gt;get('genericItemTitle')): ?&gt;
                &lt;!-- Item title --&gt;
                &lt;h3 class="catItemTitle"&gt;
                  &lt;?php if ($this-&gt;params-&gt;get('genericItemTitleLinked')): ?&gt;
                      &lt;a href="&lt;?php echo $item-&gt;link; ?&gt;"&gt;
                      &lt;?php echo $item-&gt;title; ?&gt;
                  &lt;/a&gt;
                  &lt;?php else: ?&gt;
                  &lt;?php echo $item-&gt;title; ?&gt;
                  &lt;?php endif; ?&gt;
                &lt;/h3&gt;
                &lt;?php endif; ?&gt;
                &lt;?php if($this-&gt;params-&gt;get('genericItemDateCreated')): ?&gt;
                  &lt;!-- Date created --&gt;
                  &lt;span class="genericItemDateCreated"&gt;
                      &lt;?php echo JHTML::_('date', $item-&gt;created , JText::_('DATE_FORMAT_LC10')); ?&gt;
                  &lt;/span&gt;
                  &lt;?php endif; ?&gt;
            &lt;/div&gt;
  
          &lt;/div&gt;
          &lt;!-- End K2 Item Layout --&gt;
  
          &lt;?php endforeach; ?&gt;
      &lt;/div&gt;
  
      &lt;!-- Pagination --&gt;
      &lt;?php if($this-&gt;pagination-&gt;getPagesLinks()): ?&gt;
      &lt;div class="k2Pagination"&gt;
          &lt;?php echo $this-&gt;pagination-&gt;getPagesLinks(); ?&gt;
          &lt;div class="clr"&gt;&lt;/div&gt;
          &lt;?php echo $this-&gt;pagination-&gt;getPagesCounter(); ?&gt;
      &lt;/div&gt;
      &lt;?php endif; ?&gt;
  
      &lt;?php endif; ?&gt;
  
  &lt;/div&gt;
  &lt;!-- End K2 Generic (search/date) Layout --&gt;
  </code></pre>
  
  <p>For the right value, find the corresponding id at the backend of k2 extra fields (on the right)</p>
  
  <p>For me it was <strong>id 11</strong></p>
  
  <pre><code>&lt;?php echo $extrafields[11];?&gt;
  </code></pre>