Last active
August 29, 2015 14:24
-
-
Save jmsmrgn/0494e6116bfb720c8ea9 to your computer and use it in GitHub Desktop.
WP - Truncate Field Content
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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