Using truncateChars and specifying the character limit in search results template
Permalink
Hi There,
I have no PHP knowledge but have managed to piece together a search results template that displays:
- The title
- A custom image attribute from the page
- The tags set for the page in each result
Now I would like to limit the characters of the title but don't understand how to add a character limit number.
I'm loading the text helper at the top of the template:
Then in the results foreach section have the following:
And am outputting the title like this:
But where/how do I specify the character limit for the title?
Any pointers in the right direction would be much appreciated.
Cheers
Ben
I have no PHP knowledge but have managed to piece together a search results template that displays:
- The title
- A custom image attribute from the page
- The tags set for the page in each result
Now I would like to limit the characters of the title but don't understand how to add a character limit number.
I'm loading the text helper at the top of the template:
$th = Loader::helper("text");
Then in the results foreach section have the following:
foreach($results as $r) { // for each result $cobj = Page::getByID($r->cID); // get page id for image attribute and tags $currentPageBody = $this->controller->highlightedExtendedMarkup($r->getBodyContent(), $query); // highlight the found search term in the results $title = $cobj->getCollectionName(); $title = $th->shortenTextWord($title, $controller->truncateChars);
And am outputting the title like this:
But where/how do I specify the character limit for the title?
Any pointers in the right direction would be much appreciated.
Cheers
Ben
![Remo](/files/avatars/26.jpg)
$controller->truncateChars contains the number of characters to display. If you want that to be different than the number of characters for the description, simply replace it with a number
Awesome mate - worked perfectly.
If I have these items at the start of the template:
Can any of the items in the foreach loop below be moved to the start of the template?
Or do they have to be in the loop because the title, tags and image attribute relate to each page result?
When I try moving items I get errors so assume they have to stay in the loop?
Hope that makes sense?
If I have these items at the start of the template:
<?php defined('C5_EXECUTE') or die("Access Denied."); $th = Loader::helper("text"); // load the text helper $ih = Loader::helper('image'); // load the image helper $nh = Loader::helper('navigation'); // load the navigation helper // load and process tabs $tagsBT = CollectionAttributeKey::getByHandle('recipe_tags'); // load the attribute handle of the tags to display $tagsType = $tagsBT->getAttributeType(); $tagsController = $tagsType->getController(); $tagsController->setAttributeKey($tagsBT); $targetPage = Page::getByPath('/recipes/recipe-search/'); // page to display results on when tag is clicked $searchLink = $nh->getLinkToCollection($targetPage)."?".$tagsController->field('atSelectOptionID') . '[]='; ?>
Can any of the items in the foreach loop below be moved to the start of the template?
<?php foreach($results as $r) { // for each result $cobj = Page::getByID($r->cID); // get page id for image attribute and tags $currentPageBody = $this->controller->highlightedExtendedMarkup($r->getBodyContent(), $query); // highlight the found search term in the results $title = $cobj->getCollectionName(); $title = $th->shortenTextWord($title, 40); // get page image attribute $objThumb = NULL; // assign null to image attrinute incase no image is set $objFile = $cobj->getAttribute('main_image'); // get the image attribute if ( is_object($objFile) && $objFile instanceof File && !$objFile->error) { // if an image is set $objThumb = $ih->getThumbnail($objFile, 220, 142); // load the image helper and generate a thumbnail to size } // get page tags $tagsBT = $cobj->getCollectionAttributeValue("recipe_tags"); // start outputting each result:
Viewing 15 lines of 16 lines. View entire code block.
Or do they have to be in the loop because the title, tags and image attribute relate to each page result?
When I try moving items I get errors so assume they have to stay in the loop?
Hope that makes sense?
yes, those functions are related to the current page object in the loop. Within the loop the object is called $cobj.
You can move these functions but first, you need a page object to work with. In your example, $targetPage has the same type.
This means that you could move the code and replace $cobj with $targetPage (if you want to pull the attributes from /recipes/recipe-search/
You can move these functions but first, you need a page object to work with. In your example, $targetPage has the same type.
This means that you could move the code and replace $cobj with $targetPage (if you want to pull the attributes from /recipes/recipe-search/