Home › Forums › Front-end Issues › Truncating a custom field content based on CHARACTER count › Reply To: Truncating a custom field content based on CHARACTER count
No problem @jodriscoll. 🙂
You can use strip_tags()
to remove all tags. You can also choose to allow specific tags. Here’s a link about that…
The updated code would then be:
function project_excerpt() {
global $post;
$text = get_field('project_description');
if ( '' != $text ) {
$text = strip_shortcodes( $text );
$text = apply_filters('the_content', $text);
$text = strip_tags($text);
$text = str_replace(']]>', ']]>>', $text);
$text_length = strlen($text); // Get text length (characters)
$excerpt_length = 350; // 50 desired characters
$excerpt_more = '...';
// Shorten the text
$text = substr($text, 0, $excerpt_length);
// If the text is more than 50 characters, append $excerpt_more
if ($text_length > $excerpt_length) {
$text .= $excerpt_more;
}
}
return apply_filters('the_excerpt', $text);
}
One other note: I left them in, but I’m not sure that you need the calls to apply_filters()
in there if you’re stripping everything out. You could just eliminate the first line:
$text = apply_filters('the_content', $text);
…and change the last one to return $text
.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.