Truncate function to limit Post title to specific number

Post Reply
oloh
Jr. Bludit
Posts: 3
Joined: Wed May 25, 2022 6:48 pm
Has thanked: 2 times

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
User avatar
Edi
Site Admin
Posts: 3121
Joined: Sun Aug 09, 2015 5:01 pm
Location: Zurich
Has thanked: 54 times
Been thanked: 77 times
Contact:

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; ?>
Clickwork - Websites mit Bludit | Planet Bludit - Tipps und Snippets
oloh
Jr. Bludit
Posts: 3
Joined: Wed May 25, 2022 6:48 pm
Has thanked: 2 times

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
User avatar
Edi
Site Admin
Posts: 3121
Joined: Sun Aug 09, 2015 5:01 pm
Location: Zurich
Has thanked: 54 times
Been thanked: 77 times
Contact:

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
Clickwork - Websites mit Bludit | Planet Bludit - Tipps und Snippets
Post Reply