Keyword

How to show 3 first items of Searchable Label Extra Field?

  • Fábio Jordan
  • Fábio Jordan's Avatar Topic Author
  • Offline
  • Senior Member
  • Nothing like a good coffee to make a better web
More
4 years 11 months ago #174072 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!

Please Log in or Create an account to join the conversation.

More
4 years 11 months ago #174078 by JoomlaWorks
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:
<?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
  • Fábio Jordan's Avatar Topic Author
  • Offline
  • Senior Member
  • Nothing like a good coffee to make a better web
More
4 years 11 months ago #174081 by Fábio Jordan
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!

Please Log in or Create an account to join the conversation.

More
4 years 11 months ago #174085 by JoomlaWorks
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.


Powered by Kunena Forum