Created
June 18, 2013 09:40
-
-
Save kuldeepdaftary/5804047 to your computer and use it in GitHub Desktop.
Wordpress intelligent Excerpt. Following function will create an excerpt from content where first sentence ends (with period) or if first sentence is too long it will only show number of characters defined in the function. in following case it'll show 200 characters.
This file contains 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 | |
// Variable & intelligent excerpt length. | |
function print_excerpt($length) { // Max excerpt length. Length is set in characters | |
global $post; | |
$text = $post->post_excerpt; | |
if ( '' == $text ) { | |
$text = get_the_content(''); | |
$text = apply_filters('the_content', $text); | |
$text = str_replace(']]>', ']]>', $text); | |
} | |
$text = strip_shortcodes($text); // optional, recommended | |
$text = strip_tags($text); // use ' $text = strip_tags($text,'<p><a>'); ' if you want to keep some tags | |
$text = substr($text,0,$length); | |
$excerpt = reverse_strrchr($text, '.', 1); | |
if( $excerpt ) { | |
echo apply_filters('the_excerpt',$excerpt); | |
} else { | |
echo apply_filters('the_excerpt',$text); | |
} | |
} | |
?> | |
//Code to use in template | |
<?php print_excerpt(200); ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment