Skip to content

Instantly share code, notes, and snippets.

@ajithrn
Last active August 29, 2015 13:56
Show Gist options
  • Save ajithrn/9118048 to your computer and use it in GitHub Desktop.
Save ajithrn/9118048 to your computer and use it in GitHub Desktop.
Wordpress: short_content
<?php
/**
* short_content generator
* @param string $mycontent content string
* @param string $after
* @param int $length
* @return string
*/
function short_content( $mycontent, $after = '', $length ) {
$mycontent = strip_tags( $mycontent ); //stripout tags first
if ( $mycontent ):
$mycontent = strip_shortcodes( $mycontent );
$mycontent = explode(' ',$mycontent , $length);
else: //if no specified content string, automatically fetch content according to post id
$mycontent = strip_tags( get_the_content() );
$mycontent = explode(' ', $mycontent, $length);
endif;
if (count($mycontent)>=$length) :
array_pop($mycontent);
$mycontent = implode( ' ', $mycontent ). $after;
else:
$mycontent = implode( ' ', $mycontent );
endif;
return $mycontent;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment