Hello,
How would I limit the length of the product name and product description that is displayed?
best regards
David S S
Hi David,
Only me.........
I've added the code to includes/tapestry.php and I would like to implement this on html/searchresults.php, what should I change this line to?
<?php print substr($product["description"],0,250); ?>
I've replaced the above code with:
<?php print tapestry_crop($product["description"],250); ?></p>
However, It's not working correctly,for long descriptions (more than 250 characters) it only prints the first word, if the description is shorter than 250 characters, then it will print the whole description.
thank you
Regards
Hayden
Hello Hayden,
There was actually an error in the function on this line:
$breakPos = strpos($text," ",$limit);
...it should have been:
$breakPos = strpos($text," ",$length);
(i've corrected it in the code above also)
Cheers,
David.
Hi David,
The product name and description are displayed in various places (featured products, search results, product page) - but in all cases are ultimately simple "print" statements within the relevant /html/ files. For example, in html/searchresults.php, you will find on line 16 and 18:
print $product["name"];
So for a very basic crop to 20 characters, you could replace this with:
print substr($product["name"],0,20);
However, this could of course leave you with words cut off mid-way through, which might not be desirable. As an alternative, and to avoid having to write code all over the place, I would add a new function to includes/tapestry.php that will crop a string at the next space character after a given number of characters. To do this, add the following new function to includes/tapestry.php:
function tapestry_crop($text,$length)
{
if (strlen($text) > $length)
{
$breakPos = strpos($text," ",$length);
if ($breakPos !== false)
{
$text = substr($text,0,$breakPos);
}
}
return $text;
}
(simpy insert the code at the end, immediately before the closing PHP tag)
With that in place, in place of the modification described above, you could then use:
print tapestry_crop($product["name"],20);
...and the would display the name up to the first space after 20 characters, so no words would be broken. Similarly, the main description on the product page is displayed by the following code on line 13 of html/product.php:
<p><?php print $mainProduct["description"]; ?></p>
...so that could be replace with:
<p><?php print tapestry_crop($mainProduct["description"],100); ?></p>
If you're not sure where to make the changes for any other instances of the product name or description; just let me know which place you'd like changed and I'll let you know where to replace the code...
Cheers,
David.