0

I'd like to be able to truncate the character length of the article category description.

This is the code that renders the description:

<div class="com-content-category-blog__description category-desc">
  <?php echo HTMLHelper::_('content.prepare', $child->description, '',  'com_content.category'); ?>
</div>

How can I modify the php statement to say only return the first 50 characters of the description?

many thanks.

2 Answers 2

1

You can use substr PHP function for that : https://www.php.net/manual/fr/function.substr.php

   <div class="com-content-category-blog__description category-desc">
  <?php 
     $str = HTMLHelper::_('content.prepare', $child->description, '',  'com_content.category'); 
$nbcar=50;
$str = substr($str, 0, $nbcar);
echo $str;
?>
</div> 

$nbcar is the number of chars

3
  • Thank you Seb, it works perfectly :) Dec 30, 2023 at 9:06
  • I like this as plain PHP method
    – Zollie
    Dec 30, 2023 at 21:21
  • This is not an acceptable answer. It results in broken HTML.
    – Sharky
    Dec 31, 2023 at 11:02
3

Joomla provides a string truncating function of its own that provides more options and extra processing than the substr of PHP.

Details of the function can be found in ..\libraries\src\HTML\Helpers\StringHelper.php and more information here, https://api.joomla.org/cms-4/classes/Joomla-CMS-HTML-Helpers-StringHelper.html

Your code could look something like this.

<div class="com-content-category-blog__description category-desc">
<?php $str = HTMLHelper::_('content.prepare', Joomla\CMS\HTML\Helpers\StringHelper::truncate($child->description, 50, true, false), '',  'com_content.category'); 
echo $str;
?>
</div> 

Or using the example from the answer by Seb as a template...

<div class="com-content-category-blog__description category-desc">
<?php $str = HTMLHelper::_('content.prepare', $child->description, '',  'com_content.category'); 
$str = Joomla\CMS\HTML\Helpers\StringHelper::truncate($str, 50, true, false);
echo $str;
?>
</div> 

You also have the extra functions of Abridge and truncateComplex that provide you with more options.

4
  • Thank you Irata. Weirdly, I've only just been looking at that file! And then I come back here and you've mentioned it. Annoyingly though, Joomla's method places the ellipsis outside the closing html tag so it appears on a new line, rather than at the end of the text in question. Dec 30, 2023 at 9:33
  • I like this answer as joomla specific.
    – Zollie
    Dec 30, 2023 at 21:21
  • Just a question : why Joomla has this method in so far as PHP can do it ?
    – Seb
    Dec 31, 2023 at 6:51
  • @Seb the PHP substr function is quite simple in its ability, it reduces the length of any string to a set number of characters. The Joomla function is more aware of the contents of the string in that it may contain HTML statements that may need to be closed, rather than just deleted, or it can be set to remove all HTML statements from the string. It also doesn't truncate part way through a word leaving with you some meaningless characters.
    – Irata
    Dec 31, 2023 at 9:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.