- Posts: 58
COMMUNITY FORUM
SOLVED: Tutorial for extra fields?
- Johann Scharfetter
-
Topic Author
- Offline
- Senior Member
I'm new to K2. I wonder where there is a tutorial or a video about how to create templates with extra fields?
Please could you indicate me wher to find it, if there is any?
I'm an absolute beginner in php.
THANK YOU!
Please Log in or Create an account to join the conversation.
- william white
-
- Offline
- Platinum Member
- Posts: 3722
a good place to start
Please Log in or Create an account to join the conversation.
- Johann Scharfetter
-
Topic Author
- Offline
- Senior Member
- Posts: 58
what I found out till now ist the following code:
Log in or Create an account to join the conversation.
- Lefteris
-
- Offline
- Platinum Member
- Posts: 8743
What exactly are you trying to achieve? The extra fields are being displayed automatically since you have set up accordingly the category settings.
Please Log in or Create an account to join the conversation.
- Johann Scharfetter
-
Topic Author
- Offline
- Senior Member
- Posts: 58
Lefteris Kavadas wrote: @scharfet
What exactly are you trying to achieve? The extra fields are being displayed automatically since you have set up accordingly the category settings.
Hi!
The default method despays the extra fields in a simple table format.
But I want do display the extra fields as I need it.
For instance: I have 6 extrafields that are 3 questions and 3 answers which I would like to display in a slider as you can see here.
iteplus.es/marenostrum/index.php?option=com_k2&view=item&layout=item&id=1&Itemid=134#%C2%BFc%C3%B3mo-vemos-la-homeopat%C3%ADa-en-marenostrum
An other thng: I have an extra field which should be displayed in a div with different css styles (pink background)
I want to get this: estudiohr.com/clientes/marenostrum/03.html
What I want to ask is what is the correct way to add extra fields to a k2-template? (Not using the standard display of extra fields in a table.)
I hope I could explain myself clearly.
Thank you!
======================================================
Hi William,William White wrote: getk2.org/documentation/k2class2012
a good place to start
I have watched all the vidios on this page, but none of them are informing about extra fields and how to display them in a template.
Please Log in or Create an account to join the conversation.
- Lefteris
-
- Offline
- Platinum Member
- Posts: 8743
Please Log in or Create an account to join the conversation.
- Johann Scharfetter
-
Topic Author
- Offline
- Senior Member
- Posts: 58
How to display extra fields?
Please Log in or Create an account to join the conversation.
- Johann Scharfetter
-
Topic Author
- Offline
- Senior Member
- Posts: 58
I need to know whether this code is the best way to display extra fields.
Log in or Create an account to join the conversation.
- mahdi
-
- Offline
- Junior Member
- Posts: 21
I feel your answers is reluctantlyWilliam White wrote: getk2.org/documentation/k2class2012
a good place to start
Because most of them are not efficient
Please Log in or Create an account to join the conversation.
- Johann Scharfetter
-
Topic Author
- Offline
- Senior Member
- Posts: 58
Hello,
I tried to add extra fields to a certain position in K2 item view in a new website.
What I did:
1.) In the Category-settings: the correct extra fields group is associated (Associated "Extra Fields Group")
2.) I added this code to item.php
Log in or Create an account to join the conversation.
- Napolian Stewart
-
- Offline
- New Member
- Posts: 1
Try it without the single quotes around the IDs, like this - [1] - instead of this - .
This works for me, except the tabs don't render for me.
Log in or Create an account to join the conversation.
- Johann Scharfetter
-
Topic Author
- Offline
- Senior Member
- Posts: 58
it is working now. You were right the code had errors.
For others who wants to know how to use "Extra Fields" in a personalized way in "Item view".
In "Category" > "Item View Options " > "Extra Fields" > it is necesary to choose "Show".
The extra fields will be displayed also in the standard way (in a table). This code need to be deleted forom item.php.
At the beginning of the item.php place following code.
Log in or Create an account to join the conversation.
- Odin Mayland
-
- Offline
- Platinum Member
- Posts: 404
The first screen shot shows the code working after the item list:
Attachment not found
The second screenshot shows the same code in the same category.php file but the extrafields are not showing:
Attachment not found
I can't figure out why the same code will not work in a different location.
Please Log in or Create an account to join the conversation.
- Maciej Charabin
-
- Offline
- New Member
- Posts: 9
I'm using this part of php code in my template. So I have title and subtitle (extra fields) in my k2 items.
I'd like to use it in this way:
Every item has a title and I want to display it on home page. So when user click on the main page and open k2 item, I'd like that he would see a subtitle from extra fields instead of item title. But when item has no subtitle (extra fields), he would see item title....
Maybe someone help..
Please Log in or Create an account to join the conversation.
- JoomlaWorks
-
- Offline
- Admin
- Posts: 6227
First read this on K2 templating in general: getk2.org/documentation/tutorials/174-templating-with-k2-and-the-concepts-of-sub-templates
Then edit your item.php file and locate the part where the title is shown. We'll add a small check so we can display the subtitle instead.
Create your subtitle extra field, add it to a group and make sure this group is also assigned to one or more categories in K2. Let's say this extra field is simply called "Subtitle" in which case, it will get the alias "Subtitle" as well. We will use this "alias" to directly fetch and display the subtitle value if it exists.
Find this (~line 54):
<?php echo $this->item->title; ?>
and replace with this:
<?php if($this->item->extraFields->Subtitle->value!=''): ?>
<?php echo $this->item->extraFields->Subtitle->value; ?>
<?php else: ?>
<?php echo $this->item->title; ?>
<?php endif; ?>
This will check if the Subtitle extra field is set. If it is, it will display it, otherwise it will display the K2 item title.
Simple.
For more on this neat trick read the 2.6.2 release post: getk2.org/blog/1068-k2-v262-now-available
Please Log in or Create an account to join the conversation.
- Maciej Charabin
-
- Offline
- New Member
- Posts: 9
You're the best! :)
Please Log in or Create an account to join the conversation.
- Maciej Charabin
-
- Offline
- New Member
- Posts: 9
Only one question...
Code works perfectly, but now when we have subtitle (extrafields), so it displays on the item title position (that's OK) and also on the extra fields.
Maybe someone know, how to display this subtitle (extrafield) on item title and at the same time not display in the extra fields position...
This is extra field code from item php:
<?php if($this->item->params->get('itemExtraFields') && count($this->item->extra_fields)): ?>
<!-- Item extra fields -->
<div class="itemExtraFields">
<h3><?php echo JText::_('K2_ADDITIONAL_INFO'); ?></h3>
<ul>
<?php foreach ($this->item->extra_fields as $key=>$extraField): ?>
<?php if($extraField->value != ''): ?>
<li class="<?php echo ($key%2) ? "odd" : "even"; ?> type<?php echo ucfirst($extraField->type); ?> group<?php echo $extraField->group; ?>">
<?php if($extraField->type == 'header'): ?>
<h4 class="itemExtraFieldsHeader">
<?php echo $extraField->name; ?></h4>
<?php else: ?>
<span class="itemExtraFieldsLabel"><?php echo $extraField->name; ?>:</span>
<span class="itemExtraFieldsValue"><?php echo $extraField->value; ?></span>
<?php endif; ?>
</li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
<div class="clr"></div>
</div>
<?php endif; ?>
Please Log in or Create an account to join the conversation.
- JoomlaWorks
-
- Offline
- Admin
- Posts: 6227
<?php foreach ($this->item->extra_fields as $key=>$extraField): ?>
<?php if($extraField->name=='Subtitle') continue; ?>
...
<?php endforeach; ?>
This will cause the foreach loop to skip when it comes to a "Subtitle" extra field. ;)
Please Log in or Create an account to join the conversation.
- Maciej Charabin
-
- Offline
- New Member
- Posts: 9
<?php if($this->item->params->get('itemExtraFields') && count($this->item->extra_fields)): ?>
<!-- Item extra fields -->
<div class="itemExtraFields">
<h3><?php echo JText::_('K2_ADDITIONAL_INFO'); ?></h3>
<ul>
<?php foreach ($this->item->extra_fields as $key=>$extraField): ?>
<?php foreach ($this->item->extra_fields as $key=>$extraField): ?>
<?php if($extraField->name=='Podtytul') continue; ?>
<?php endforeach; ?>
<?php if($extraField->value != ''): ?>
<li class="<?php echo ($key%2) ? "odd" : "even"; ?> type<?php echo ucfirst($extraField->type); ?> group<?php echo $extraField->group; ?>">
<?php if($extraField->type == 'header'): ?>
<h4 class="itemExtraFieldsHeader"><?php echo $extraField->name; ?></h4>
<?php else: ?>
<span class="itemExtraFieldsLabel"><?php echo $extraField->name; ?>:</span>
<span class="itemExtraFieldsValue"><?php echo $extraField->value; ?></span>
<?php endif; ?>
</li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
But extra field wchich namie is "Podtytul" still exist in extra fields position... :( Please help...
Please Log in or Create an account to join the conversation.
- Krikor Boghossian
-
- Offline
- Platinum Member
- Posts: 15920
<?php foreach ($this->item->extra_fields as $key=>$extraField): ?>
Please Log in or Create an account to join the conversation.