Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WP - Truncate Field Content
function truncate_field($title) {
global $post;
$text = get_field($title);
if ( '' != $text ) {
$text = strip_shortcodes( $text );
$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]>>', $text);
$text_length = strlen($text); // Get text length (characters)
$excerpt_length = 100; // 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);
}
<!-- add below to template -->
<?php echo truncate_field('field_name'); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment