Skip to content

Instantly share code, notes, and snippets.

@geilt
Last active December 12, 2015 08:59
Show Gist options
  • Save geilt/4748326 to your computer and use it in GitHub Desktop.
Save geilt/4748326 to your computer and use it in GitHub Desktop.
Better Excerpt functionality for Wordpress. Just paste functions.php in Wordpress to use.
/**
* Excerpt by Alexander Conroy
* Copyright 2012 Esotech Inc.
* MIT License
* http://opensource.org/licenses/MIT
*/
<?php
if(!function_exists( 'excerpt' ) ):
function excerpt( $size = '165', $content = NULL, $echo = TRUE, $ellipsis = TRUE, $wrap = TRUE ) {
if(!$content):
$content = get_the_content();
endif;
$content = apply_filters( 'the_content', $content );
$content = str_replace( ']]>', ']]&gt;', $content );
$content = strip_tags( $content );
$content = ltrim( $content);
if(strlen($content) > $size):
$space = strpos( $content, " ", $size);
endif;
if($space):
$content = substr( $content, 0, $space );
endif;
if($ellipsis):
$content = $content . "...";
endif;
if($wrap):
$content = '<p>' . $content . '</p>';
endif;
if( $echo ):
echo $content;
else:
return $content;
endif;
}
endif;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment