Truncate Product Title in Searchresults.php
Submitted by yoyd on Fri, 2010-03-05 09:13.Price Tapestry
Can you tell me how to truncate the product title in searchresults.php, if it is a really long title. For example truncating the title to 20 characters and adding "...." at the end.
Thanks!
Hi,
Sure - here's a neat way to truncate the title that won't break any words up; by splitting the string on the first space after the limit. In html/searchresults.php look for the following code on line 6:
<?php foreach($searchresults["products"] as $product): ?>...and REPLACE that with:
<?php foreach($searchresults["products"] as $product): ?><?php
$breakLimit = 20;
$displayName = $product["name"];
if (strlen($displayName) > $breakLimit)
{
$breakOffset = strpos($displayName," ",$breakLimit);
if ($breakOffset !== false)
{
$displayName = substr($displayName,0,$breakOffset)."...";
}
}
?>
Then on lines 16 and 18 where the name is actually displayed, REPLACE
$product["name"]...with:
$displayNameThis will ensure that the full title still appears where it used as an alt tag.
Cheers,
David.