Last active
August 29, 2015 13:56
-
-
Save ajithrn/9118048 to your computer and use it in GitHub Desktop.
Wordpress: short_content
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 | |
/** | |
* 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