Created
December 30, 2015 05:46
-
-
Save zeshanshani/eae284d5cbea291268ef to your computer and use it in GitHub Desktop.
Limit WordPress excerpt length by character and keep last word complete, i.e., instead of 'compat...' show full last word, i.e. 'compatibility...'.
This file contains hidden or 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
// Excerpt - Limit Length by Characters and Keep Last Word Complete. | |
// Solution from: | |
// https://wordpress.org/support/topic/limit-excerpt-length-by-characters#post-2248039 | |
// ================================================================================ | |
function excerpt_character_length( $excerpt ){ | |
$permalink = get_permalink($post->ID); | |
$excerpt = get_the_content(); | |
$excerpt = preg_replace(" (\[.*?\])",'',$excerpt); | |
$excerpt = strip_shortcodes($excerpt); | |
$excerpt = strip_tags($excerpt); | |
$excerpt = substr($excerpt, 0, 40); | |
$excerpt = substr($excerpt, 0, strripos($excerpt, " ")); | |
$excerpt = trim(preg_replace( '/\s+/', ' ', $excerpt)); | |
$excerpt = $excerpt . '... <a class="more-link" href="' . $permalink . '">Read More</a>'; | |
return $excerpt; | |
} | |
add_filter('the_excerpt', 'excerpt_character_length'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment