laurenclark
12/1/2014 - 2:42 PM

Custom Field Wysiwyg Excerpt

Custom Field Wysiwyg Excerpt

//Get the excerpt for main_content

function custom_field_excerpt() {
    global $post; //Contains data from current post in the loop.
    $text = get_field('main_content'); //Cache the custom field to variable.
    if ( '' != $text ) { //Check if it's empty.
        $text = strip_shortcodes( $text ); //Remove any shortcodes as this is just an excerpt
        $text = apply_filters('the_content', $text); //Apply the_content to $text
        $text = str_replace(']]>', ']]>', $text);
        $excerpt_length = 20; // 20 words
        $excerpt_more = apply_filters('excerpt_more', ' ' . '[...]'); //Apply the excerpt 
        $text = wp_trim_words( $text, $excerpt_length, $excerpt_more ); // Trim words and add on the excerpt
    }
    return apply_filters('the_excerpt', $text);