user13286
user13286

Reputation: 3075

PHP character limit

I am using a custom function in my Wordpress theme to limit the length of the excerpt that is displayed. Currently my function is limiting the number of words but I would like to modify it so that it limits the number of characters instead. Here is what I have for my custom function:

function limit_words($string, $word_limit) {
    $words = explode(' ', $string);
    return implode(' ', array_slice($words, 0, $word_limit));
}

And I'm calling it in my template like so:

<?php echo limit_words(get_the_excerpt(), '100'); ?>

How can I modify this function to restrict the content to 100 characters rather than words?

Upvotes: 0

Views: 83

Answers (1)

Don&#39;t Panic
Don&#39;t Panic

Reputation: 41810

You don't need a custom function to limit characters. A built-in function will work just fine.

Just use substr in your template.

<?php echo substr(get_the_excerpt(), 0, 100); ?>

Upvotes: 1

Related Questions