0

Some of my posts' titles have leading words, followed by a colon. I am removing the leading words and the column of every post title in the loop with the following:

<?php
  $title = get_the_title();
  $title_array = explode(':', $title);
  $first_word = $title_array[1];
  echo $first_word;
?>

How can I also limit the total length of the remaining title by character count?

2 Answers 2

1
$title = get_the_title();
$title_array = explode(':', $title);
$first_word = $title_array[1];
$total_length_limit = 100;
echo mb_substr( $first_word, 0, $total_length_limit ) . '...';
2
  • 1
    You really want mb_substr().
    – fuxia
    Commented Jun 4, 2012 at 18:34
  • Is there a way that I can append ellipsis to the end?
    – Ryan
    Commented Jun 4, 2012 at 18:56
1

I think that if you want to get first word you should set array index to zero: $first_word = $title_array[0];

And regarding your question:

$char_limit = 20;
$out = array_slice($title_array, 1);
$out = join(':',$out);
$out = substr($out, 0, $char_limit).'...';

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.