开发者

How to amend my php function to add extra text?

Note that I used this with wordpress.

I added this function to functions.php:

function string_limit_words($string, $word_limit)
    { 
  $words = explode(' ', $string, ($word_limit + 1));
  if(count($words) > $word_limit)
  array_pop($words);
  return implode(' ', $words);
}

I added this to my html:

<?php if ( $woo_options['woo_post_content_home'] == "true" ) the_content();开发者_开发百科 else $excerpt = get_the_excerpt(); echo string_limit_words($excerpt,38); ?>

What this does is shortens the text to the number of words specified (in this sample its 38). What I want to do is add [...] after those 38 words. Any suggestions on how I can do this?

Regards,


Change it to

function string_limit_words($string, $word_limit)
    { 
  $words = explode(' ', $string, ($word_limit + 1));
  if(count($words) > $word_limit)
  {
      array_pop($words);
      $words[] = '[...]';
  }
  return implode(' ', $words);
}


Uh...

function string_limit_words($string, $word_limit)
    { 
  $words = explode(' ', $string, ($word_limit + 1));
  if(count($words) > $word_limit)
  array_pop($words);
  return implode(' ', $words) . '...';
}


Just

echo string_limit_words($excerpt,38) . " ...";

should suffice.


You can modify the function like this so it will add the ellipses only when shorten the phrase.

function string_limit_words($string, $word_limit)
    { 
  $ellipses = '';
  $words = explode(' ', $string, ($word_limit + 1));
  if(count($words) > $word_limit) {
    array_pop($words);
    $ellipses = ' ...';
  }
  $newString = implode(' ', $words) + $ellipses;
  return $newString
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜