
Originally Posted by
modernW
For this I'd suggest using the
substr() function. It accepts three arguments, the first being the strings content, the second is the start value (number of characters through the content), and the third (optional) is the number of characters to grab. It can be easily applied like so:
Although that will work, keep in mind that WordPress stores HTML tags too. So you may be grabbing the first 10 characters that includes an opening <p> tag and not a closing tag.
Personally, I think you need to use the Excerpt field in WordPress and pull that field instead of the content, or you need to at least strip all of the tags from the content before grabbing the first 10 characters.
PHP Code:
$content = strip_tags(apply_filters( 'the_content', $content ));
$content = substr($content, 0, 10); #gets the first 10 characters
Another technique can use str_word_count
PHP Code:
$content = strip_tags(apply_filters( 'the_content', $content ));
$words = str_word_count($content, 1);
$wordChunks = array_chunk($words, 50); // chunk the words into groups of 50
$first50words = implode(' ', $words[0]); // makes a paragraph of the first 50 words
Bookmarks