COMMUNITY FORUM
- Forum
- K2 Community Forum
- English K2 Community
- How to show 3 first items of Searchable Label Extra Field?
How to show 3 first items of Searchable Label Extra Field?
- Fábio Jordan
- Topic Author
- Offline
- Senior Member
- Nothing like a good coffee to make a better web
Less
More
4 years 11 months ago #174072
by Fábio Jordan
How to show 3 first items of Searchable Label Extra Field? was created by Fábio Jordan
Hello, everyone!
So, I'm trying to output only 3 items from a searchable label. Normally, this type of extrafield shows all the words and respectives links. Usually, the code to output an extrafield is:
<?php echo $this->item->extraFields->cast->value; ?>
But sometimes (like in the category_item.php file) I don't want to output all the items, since some articles have lots of items in this field. Is it possible to use array_slice or other command to split the extra field and output only 3 first items?
Thanks in advance!
So, I'm trying to output only 3 items from a searchable label. Normally, this type of extrafield shows all the words and respectives links. Usually, the code to output an extrafield is:
<?php echo $this->item->extraFields->cast->value; ?>
But sometimes (like in the category_item.php file) I don't want to output all the items, since some articles have lots of items in this field. Is it possible to use array_slice or other command to split the extra field and output only 3 first items?
Thanks in advance!
Please Log in or Create an account to join the conversation.
- JoomlaWorks
- Offline
- Admin
Less
More
- Posts: 6218
4 years 11 months ago #174078
by JoomlaWorks
Fotis / JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Replied by JoomlaWorks on topic How to show 3 first items of Searchable Label Extra Field?
The output is a string that contains links. What you can do is parse that, turn it into an array and then place a limit to the output.
So I would do this:
So I would do this:
<?php
$limit = 3;
$parts = explode("</a>", $this->item->extraFields->cast->value);
if (!empty($parts) && count($parts)) {
$output = array();
foreach($parts as $key => $url) {
if (trim($url) != '' && $key < $limit) {
$output[] = $url.'</a>'; // Rebuild the URL and add it to the output array
}
}
echo implode('', $output); // Merge the array to a string
}
Fotis / JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Please Log in or Create an account to join the conversation.
- Fábio Jordan
- Topic Author
- Offline
- Senior Member
- Nothing like a good coffee to make a better web
4 years 11 months ago #174081
by Fábio Jordan
Replied by Fábio Jordan on topic How to show 3 first items of Searchable Label Extra Field?
Wow! Works like charm!
I had this code before, but lost it because a server issue and wasn't remembering if the extrafield worked as string or array, passed a few hours trying to deal with it as array. Now everything makes sense :)
Anyway, thanks for your help Fotis!
Have a great weekend!
I had this code before, but lost it because a server issue and wasn't remembering if the extrafield worked as string or array, passed a few hours trying to deal with it as array. Now everything makes sense :)
Anyway, thanks for your help Fotis!
Have a great weekend!
Please Log in or Create an account to join the conversation.
- JoomlaWorks
- Offline
- Admin
Less
More
- Posts: 6218
4 years 11 months ago #174085
by JoomlaWorks
Fotis / JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Replied by JoomlaWorks on topic How to show 3 first items of Searchable Label Extra Field?
You're welcome.
Fotis / JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Please Log in or Create an account to join the conversation.
- Forum
- K2 Community Forum
- English K2 Community
- How to show 3 first items of Searchable Label Extra Field?