0

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?

1 Answer 1

1

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); ?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.