Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 1 down vote accepted
$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 ) . '...';
share|improve this answer
1  
You really want mb_substr(). –  toscho Jun 4 '12 at 18:34
    
@toscho thanks, fixed. –  Eugene Manuilov Jun 4 '12 at 18:36
    
That worked perfectly. Thank you so much! –  Ryan Jun 4 '12 at 18:39
    
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).'...';
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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