Created
October 6, 2017 13:00
-
-
Save aderaaij/bd6cb757b81a7444a5c5ad28259f87be to your computer and use it in GitHub Desktop.
Creates a WordPress excerpt based on the amount of characters you want it to have
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
<?php | |
/* = Limit Excerpt | |
/* Usage: echo get_excerpt(250) | |
-------------------------------------------------------------- */ | |
function get_excerpt($limit, $source = null){ | |
if($source == "content" ? ($excerpt = get_the_content()) : ($excerpt = get_the_excerpt())); | |
$excerpt = preg_replace(" (\[.*?\])",'',$excerpt); | |
$excerpt = strip_shortcodes($excerpt); | |
$excerpt = strip_tags($excerpt); | |
$excerpt = substr($excerpt, 0, $limit); | |
$excerpt = substr($excerpt, 0, strripos($excerpt, " ")); | |
$excerpt = trim(preg_replace( '/\s+/', ' ', $excerpt)); | |
$excerpt = $excerpt.'...'; | |
return $excerpt; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment