Skip to content

Instantly share code, notes, and snippets.

@ashokmhrj
Created December 29, 2015 10:42
Show Gist options
  • Save ashokmhrj/697dca717aedc434c332 to your computer and use it in GitHub Desktop.
Save ashokmhrj/697dca717aedc434c332 to your computer and use it in GitHub Desktop.
Wordpress excerpt html tags with formatting
<?php
/**
* this wordpress script is for accepting the excerpt function with html format along with css design.
*/
// child custom excerpt length
function custom_excerpt_length( $length ) {
return 199;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
/**
* function to accept html format with given css design
*/
function custom_wp_trim_excerpt($text) {
$raw_excerpt = $text;
if ( '' == $text ) {
//Retrieve the post content.
$text = get_the_content('');
$text = strip_shortcodes( $text );
$text = apply_filters('the_content', $text);
$text = str_replace(']]&gt;', ']]&gt;', $text);
// the code below sets the excerpt length to 55 words. You can adjust this number for your own blog.
$excerpt_length = apply_filters('excerpt_length');
// the code below sets what appears at the end of the excerpt, in this case ...
$excerpt_more = apply_filters('excerpt_more');
$words = preg_split("/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY);
if ( count($words) > $excerpt_length ) {
array_pop($words);
$text = implode(' ', $words);
$text = force_balance_tags( $text );
$text = $text . $excerpt_more;
} else {
$text = implode(' ', $words);
}
}
return apply_filters('wp_trim_excerpt', $text, $raw_excerpt);
}
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'custom_wp_trim_excerpt');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment