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:

  $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?

$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 ) . '...';
  • 1
    You really want mb_substr(). – fuxia Jun 4 '12 at 18:34
  • Is there a way that I can append ellipsis to the end? – Ryan Jun 4 '12 at 18:56

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, privacy policy and cookie policy

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