0

NOTE PLEASE : for all of you who will mark this as a duplicate

THE ISSUE WAS NOT SOLVED USING CODE FROM THAT TOPIC. PLEASE DON'T MARK THIS AS A DUPLICATE BUT TRY TO HELP ME OUT. I'D REALLY APPRECIATE IT :)

Let me explain my issue.

I got a WordPress with 10,000 posts and like +6k of them have tittles longer than 65 characters which affects SEO.

I'm using this code to limit titles to 65 characters:

function tokenTruncate($title, $your_desired_width) {
$parts = preg_split('/([\s\n\r]+)/', $title, null, PREG_SPLIT_DELIM_CAPTURE);
$parts_count = count($parts);

$length = 0;
$last_part = 0;
for (; $last_part < $parts_count; ++$last_part) {
$length += strlen($parts[$last_part]);
if ($length > $your_desired_width) { break; }
}

return implode(array_slice($parts, 0, $last_part));

$your_desired_width = 65;

if (strlen($title) > $your_desired_width) {
$title = wordwrap($title, $your_desired_width);
$title = substr($title, 0, strpos($title, "\n")).'...';
return $title;
}

add_filter('the_title','tokenTruncate');
legacy.so
  • 35
  • 8
  • https://developer.wordpress.org/reference/functions/wp_trim_words/ – Lawrence Cherone Feb 17 '18 at 09:48
  • @LawrenceCherone Thank you for your suggestion, but i haven't been able to achieve a solution. I've checked your suggestion before and all possible solutions. I'm a novice in this and maybe i'm doing some small mistakes which doesn't get the code right. I'd really appreciate it if you take a look and give me a suggestion. – legacy.so Feb 17 '18 at 09:50
  • Sorry but; `$title = substr( $title, 0, 65 ) . '...';` wont cut it. The closed question works if you use it right, see: https://3v4l.org/FWTbC - or you could simply use the wordpress function/ – Lawrence Cherone Feb 17 '18 at 09:52
  • @LawrenceCherone then i must be total retarded. I updated original post ( yeah, the code is not working for my stupid as* ) take a look there please. – legacy.so Feb 17 '18 at 10:16
  • Use this: https://3v4l.org/Iodfe – Lawrence Cherone Feb 17 '18 at 10:29

0 Answers0