Archived topic
Custom Excerpt Function Help?
1 reply · Started by Daniel on January 17, 2017
I am using a custom function to grab text from the content, so that I don't have to fill in the excerpt field. I am mainly using this for custom post types.
It is working, but I can't seem to overwrite GP's read more link.
Function below:
function get_excerpt(){
$excerpt = get_the_content();
$excerpt = preg_replace(" (\[.*?\])",'',$excerpt);
$excerpt = strip_shortcodes($excerpt);
$excerpt = strip_tags($excerpt);
$excerpt = substr($excerpt, 0, 120);
$excerpt = substr($excerpt, 0, strripos($excerpt, " "));
$excerpt = trim(preg_replace( '/\s+/', ' ', $excerpt));
$excerpt = '<p>'.$excerpt.'... More</p>';
return $excerpt;
}
I would go with the wp_trim_words() function for custom excerpts: https://codex.wordpress.org/Function_Reference/wp_trim_words