COMMUNITY FORUM
Limiting Extra FieldsDisplay
- CuppaJoe
- Topic Author
- Offline
- New Member
- Share The Warmth
I have 15 extra field codes to add to each item in a category.
Is there a way, using some custom code added to my K2 template, that will allow limiting the number of extra fields that are shown in the category_item.
If I set my category to show extra fields, then all 15 will be displayed, but what if I want to display just the first five, or even specific extra field can this be done by their alias.
Any help is appreciated.
Joe
Regards,
Joe,
ShareTheWarmth
Please Log in or Create an account to join the conversation.
- Hardkiffeur
- Offline
- Senior Member
- Posts: 56
You could rewrite how the extrafields's list is publish in the itemExtraFields bloc (on the category_item.php) , just after
<div class="itemExtraFields">
<ul>
Exemple : First extrafield's alias is EXTRA1 and a second EXTRA2 (alias could be read on the K2 item's list)
<li class="odd"><span class="itemExtraFieldsLabel"><?php echo $this->item->extraFields->EXTRA1->name; ?></span><span class="itemExtraFieldsValue"><?php echo $this->item->extraFields->EXTRA1->value; ?></span></li>
<li class="even"><span class="itemExtraFieldsLabel"><?php echo $this->item->extraFields->EXTRA2->name; ?></span><span class="itemExtraFieldsValue"><?php echo $this->item->extraFields->EXTRA2->value; ?></span></li>
and so on with the Extrafields, Alias you want to show ;)
I assume you already do an override template and assigne it t the categorie you want to work with ;)
Let me know
Please Log in or Create an account to join the conversation.
- CuppaJoe
- Topic Author
- Offline
- New Member
- Share The Warmth
That looks like it would work lovely.
Yes, always use over rides.
Joe
Regards,
Joe,
ShareTheWarmth
Please Log in or Create an account to join the conversation.
- Nikos
- Offline
- New Member
- Posts: 17
Hello and hope to get this work,hardkiffeur wrote: Hi Cuppa,
You could rewrite how the extrafields's list is publish in the itemExtraFields bloc (on the category_item.php) , just after
<div class="itemExtraFields">
<ul>
Exemple : First extrafield's alias is EXTRA1 and a second EXTRA2 (alias could be read on the K2 item's list)
<li class="odd"><span class="itemExtraFieldsLabel"><?php echo $this->item->extraFields->EXTRA1->name; ?></span><span class="itemExtraFieldsValue"><?php echo $this->item->extraFields->EXTRA1->value; ?></span></li>
<li class="even"><span class="itemExtraFieldsLabel"><?php echo $this->item->extraFields->EXTRA2->name; ?></span><span class="itemExtraFieldsValue"><?php echo $this->item->extraFields->EXTRA2->value; ?></span></li>
and so on with the Extrafields, Alias you want to show ;)
I assume you already do an override template and assigne it t the categorie you want to work with ;)
Let me know
So I would be pleased if you could tell me where exactly should I make the copy of category_item.php in order to get this override to work. What if I needed those specific extra fields to be shown in every item in the category view? I inserted the code that you suggested and nothing happened. I checked which template I'm using for the category that I want to implement this and it's the default. So I have to change the file which is into /components/com_k2/templates/default/category_item.php . Could you please explain it in more details? Sorry for asking something like that but I am new to K2 but I have a bit of knowledge in php.
Thank you in advanced.
Please Log in or Create an account to join the conversation.
- Lefteris
- Offline
- Moderator
- Posts: 8743
Please take first a read regarding K2 templating at getk2.org/documentation/tutorials/174-templating-with-k2-and-the-concepts-of-sub-templates
JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Please Log in or Create an account to join the conversation.
- Nikos
- Offline
- New Member
- Posts: 17
Hello Lefteri,Lefteris Kavadas wrote: @Nikos
Please take first a read regarding K2 templating at getk2.org/documentation/tutorials/174-templating-with-k2-and-the-concepts-of-sub-templates
I have read this documentation already and it didn't work for me that way. I had to create another folder named template into templates/mytemplate/html/com_k2/TEMPLATE/default or blog in order to select it as a template of my category in k2 back-end. So I did that and still if I make any changes into category_item.php for instance it does not affect anything. I tried to delete this file and still nothing happened.
Let's try something else. If I wanted to make some changes in the category view, the file that I should change is the category_item.php which is in the path components/com_k2/template/default provided that I have selected default template in k2 back-end for the category template. Right?
Please Log in or Create an account to join the conversation.
- Nikos
- Offline
- New Member
- Posts: 17
Thank you for your help though.
Please Log in or Create an account to join the conversation.
- Lefteris
- Offline
- Moderator
- Posts: 8743
/templates/YOURTEMPLATE/html/com_k2/default/category.php
/templates/YOURTEMPLATE/html/com_k2/default/category_item.php
depending on the part you wish to change. Note that when you are making a menu link to multiple categories then the template needs to be set in the menu link settings.
JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Please Log in or Create an account to join the conversation.
- CuppaJoe
- Topic Author
- Offline
- New Member
- Share The Warmth
I used a slightly simpler/dirty way some might say.
My example below was added to the item.php as follows:
<?php foreach ($this->item->extra_fields as $key=>$extraField): ?>
<?php if($extraField->value): ?>
<li class="type<?php echo ucfirst($extraField->alias); ?> group<?php echo $extraField->group; ?>">
<span class="itemExtraFieldsLabel"><?php echo $extraField->name; ?>:</span>
<span class="itemExtraFieldsValue"><?php echo $extraField->value; ?></span>
</li>
<?php endif; ?>
<?php endforeach; ?>
The key is adding the extra fields alias to the list class, this way a custom class can be coded for that extra field and hidden or restyled as required.
Thanks to Simon at K2Joom for the suggestion.
Regards,
Joe,
ShareTheWarmth
Please Log in or Create an account to join the conversation.
- Mohamed Abdelaziz
- Offline
- Platinum Member
- Joomla Developer
I thought in a modification to your way, that can be used in this case, without having the fields html in the document but hiding them with CSS.
You can create an array with the field ids either to show or to hide, which is shorter.
Lets assume that the shorter list is the fields to show, then the code will be like this:
<?php $fieldsToShow = array(1,5,7,13); //this is an array of fields keys/ids to show ?>
<?php foreach ($this->item->extra_fields as $key=>$extraField): ?>
<?php if(in_array($key, $fieldsToShow)): ?>
<li class="type<?php echo ucfirst($extraField->alias); ?> group<?php echo $extraField->group; ?>">
<span class="itemExtraFieldsLabel"><?php echo $extraField->name; ?>:</span>
<span class="itemExtraFieldsValue"><?php echo $extraField->value; ?></span>
</li>
<?php endif; ?>
<?php endforeach; ?>
This way, only the html code of the fields to show will be there.
Also it easy to modify the $fieldsToShow array if you changed your mind.
Multiple Extra Fields Groups for K2
AutoMeta for K2
Chained Fields for K2
More K2 Extensions In My Extensions Store
Please Log in or Create an account to join the conversation.