Skip to content

Instantly share code, notes, and snippets.

@jmsmrgn
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmsmrgn/0494e6116bfb720c8ea9 to your computer and use it in GitHub Desktop.
Save jmsmrgn/0494e6116bfb720c8ea9 to your computer and use it in GitHub Desktop.
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