Skip to content

Instantly share code, notes, and snippets.

@laurenclark
Created December 1, 2014 14:42
Show Gist options
  • Save laurenclark/7d57164528850f2bd836 to your computer and use it in GitHub Desktop.
Save laurenclark/7d57164528850f2bd836 to your computer and use it in GitHub Desktop.
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);
@laurenclark
Copy link
Author

WordPress custom field excerpt.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment