I am currently trying to tweak the item.php file into displaying the extra fields separately instead of in a group. I found som code snippets here & there, not really helping as I think they may be outdated.
For instance: This page: http://steffenjungersen.moloch.dk/nugent-be-goode
I would like "Info" to show on top of the item underneath the bold intro-text. Also, I would like the "Karakter" (a drop down menu extra field) to display as stars from 1-6
Using the category names I gave these extra fields, I came up with this for the "Karakter" one in item.php:
<?php if(isset($this->item->extra_fields[rating]) && ($this->item->extra_fields[rating] >= 0 || $this->item->extra_fields[rating] <=6)): ?>
<span class="starsbox stars<?php echo $this->item->extra_fields[rating]; ?>"></span>
<?php endif; ?>
And then this in my k2.css file:
.starsbox {
width: 96px;
height: 16px;
display: inline-block;
background: url(images/stars.png) no-repeat;
}
.stars6 {
background-position: 0px 0px;
}
.stars5 {
background-position: -16px 0px;
}
...etc
That didn't work.
Similarly, I tried calling the "Info" field into an independent position and placing it at the top. No reaction.
So i removed the whole item->extra_fields as $key=>$extraField): ?> block and then the extra fields just went away.
Can anyone point me in the right direction here?
Thank you :-)
-astrid
Okay. I got some help from my friends. Here's how far it got us (if anyone should wonder).
In order to have the extra fields displayed independently, you need to split them up. So replace the "foreach ..." line in you item.php with this:
<!-- START: Call to prepare extra fields -->
<?php
//convertArray to use ids as key
$extrafields = array();
foreach($this->item->extra_fields as $item)
{
$extrafields[$item->id] = $item->value;
}
?>
<!-- END: Call to prepare extra fields -->
Then, to call the extra field you need in, do this:
<?php if(isset($extrafields[X]) === true):?>
<?php echo $extrafields[X]; ?>
<?php endif; ?>
Where X is the numeric ID of the extra field.
Now here comes the fun part. I wanted to create a 1-6 stars rating system for my client who is a music journalist. He should be able to select a rating from a dropdown, and this value should be displayed as stars in the item view.
I decided to use the css and sprite based article rating system which comes with K2 - then I could "recycle" the nice star images and the css already created.
Heres how it looks:
<?php if(isset($extrafields[3]) === true):?>
<ul class="itemRatingList">
<li class="itemCurrentRating" id="itemCurrentRating<?php echo $this->item->id; ?>" style="width:<?php echo round($extrafields[3]*100/6); ?>%;"></li>
<li><?php if(isset($extrafields[3]) == 1):?><a href="#" title="<?php echo JText::_('K2_1_STAR_OUT_OF_5'); ?>" class="one-star">1</a></li><?php endif; ?>
<li><?php if(isset($extrafields[3]) == 2):?><a href="#" title="<?php echo JText::_('K2_2_STARS_OUT_OF_5'); ?>" class="two-stars">2</a></li><?php endif; ?>
<li><?php if(isset($extrafields[3]) == 3):?><a href="#" title="<?php echo JText::_('K2_3_STARS_OUT_OF_5'); ?>" class="three-stars">3</a></li><?php endif; ?>
<li><?php if(isset($extrafields[3]) == 4):?><a href="#" title="<?php echo JText::_('K2_4_STARS_OUT_OF_5'); ?>" class="four-stars">4</a></li><?php endif; ?>
<li><?php if(isset($extrafields[3]) == 5):?><a href="#" title="<?php echo JText::_('K2_5_STARS_OUT_OF_5'); ?>" class="five-stars">5</a></li><?php endif; ?>
<li><?php if(isset($extrafields[3]) == 6):?><a href="#" title="<?php echo JText::_('K2_6_STARS_OUT_OF_5'); ?>" class="six-stars">6</a></li><?php endif; ?>
</ul>
<?php endif; ?>
When I at some point can manage to get my hands down, I will look into replacing the a tags with span or something. And I might get around to correcting the text thing. But now it works.
Best regards,
Astrid