Page 1 of 1

Truncate function to limit Post title to specific number

Posted: Wed May 25, 2022 7:02 pm
by oloh
Please I need help on how to truncate post titles?

Truncating, or shortening, so I can control the length of my blog post titles across the website. I'm using the BLEKATHLON theme. Thank you

Re: Truncate function to limit Post title to specific number

Posted: Wed May 25, 2022 7:35 pm
by Edi
You can use the PHP function substr:

https://www.php.net/manual/de/function.substr.php

For example add the following to the templates home.php and page.php (in the directory bl-themes/blekathon/php):

Code: Select all

<?php
   $title = $page->title();
   $title = substr($title, 0, 10);
?>
Then replace

Code: Select all

<?php echo $page->title(); ?>
with

Code: Select all

<?php echo $title; ?>

Re: Truncate function to limit Post title to specific number

Posted: Wed May 25, 2022 8:00 pm
by oloh
Thanks alot. It works.

Final one, How can I add '...' at the end please and only let it shows when it's more than 10.

Also, instead of character count I want to truncate/count word

Thanks

Re: Truncate function to limit Post title to specific number

Posted: Wed May 25, 2022 8:24 pm
by Edi
oloh wrote: Wed May 25, 2022 8:00 pm Final one, How can I add '...' at the end please and only let it shows when it's more than 10.

Code: Select all

<?php
   $title = $page->title();
   if (strlen($title) > 10) {
      $title = substr($title, 0, 10).'...';
   }				}
?>
Also, instead of character count I want to truncate/count word
This question is not Bludit related. ;-)

But it can be done with PHP:

https://www.php.net/manual/de/function. ... -count.php