So I have been extremely dissatisfied with the handling of excerpts and I went looking for solutions to how to use different length excerpts in different places. I cam across a php Class written by Baelor Rae on stackoverflow.com. It worked great, but I thought it could go further.
So i modified it to also change the [...] text, add a permalink and tell it which html tags you would like allowed.
instead of using the_excerpt() you'd use kia_excerpt() (i also made a get_kia_excerpt() that returns the value.... with the following parameters
length- can be passed short (25), regular(55), long(100) xlong (200) or any custom number
a truncation character/text- the wordpress default is 'regular' [...], none, and ellipse. you can also pass in a custom text that will be turned into a permalink to the rest of the post.
and
strip tags- the WP default is to strip everything, but you can pass in <a><strong>
or hopefully whatever tags you'd like left
so in sample usage you might use:
kia_excerpt('short','Read More','<strong>');
to get short excerpts with a permalink and bold tags allowed.
to get an excerpt of 15 words, you'd use kia_excerpt(15) and so on
i'd like to make it sentence aware for version 1.0, but that could be a bit beyond me. there is a plugin that already does that so i might be able to borrow and blend.
i haven't tested it extensively, so let me know if you come across any problems or can think of any improvements.
you'd either paste it all into your functions.php file (or i usually keep it separate and then just include the file from my functions.php file as things tend to get a little crowded in functions.php). without further blathering, i present the kia_excerpt
<?php
/*-----------------------------------------------------------------------------------*/
/* KIA TOTAL CONTROL OF EXCERPTS
/* by Kathy Darling
/* www.kathyisawesome.com (coming eventually)
/*-----------------------------------------------------------------------------------*/
//Replace wp_trim_excerpt with addition of filter for strip_tags
function improved_trim_excerpt($text) {
$raw_excerpt = $text;
if ( '' == $text ) {
$text = get_the_content('');
$text = strip_shortcodes( $text );
$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]>', $text);
$tags = apply_filters('strip_tags', NULL);
$text = strip_tags($text, $tags);
$excerpt_length = apply_filters('excerpt_length', 55);
$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 = $text . $excerpt_more;
} else {
$text = implode(' ', $words);
}
}
return apply_filters('improved_trim_excerpt', $text, $raw_excerpt);
}
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'improved_trim_excerpt');
/* Class that enables excerpt length parameter */
/* called via kia_excerpt('length') */
class Excerpt {
// Default length (by WordPress)
public static $length = 55;
// Default more (by WordPress)
public static $more = '[...]';
// Default tags (none)
public static $tags = '';
// So you can call: kia_excerpt('short');
public static $types = array(
'short' => 25,
'regular' => 55,
'long' => 100,
'xlong' => 200,
);
// So you can call: kia_excerpt('short','ellipse');
public static $more_types = array(
'none' => '',
'regular' => '[...]',
'ellipse' => '...',
);
/**
* Sets the length for the excerpt,
* then it adds the WP filter
* And automatically calls the_excerpt();
*
* @param string $new_length
* @return void
* @author Baylor Rae
*/
public static function filter($new_length = 55, $new_more='[...]',$new_tags='') {
Excerpt::$length = $new_length;
Excerpt::$more = $new_more;
Excerpt::$tags = $new_tags;
add_filter('strip_tags', 'Excerpt::new_tags');
add_filter('excerpt_length', 'Excerpt::new_length',98);
add_filter('excerpt_more', 'Excerpt::new_more',99);
return Excerpt::output();
}
// Tells WP the new length
public static function new_length() {
if( isset(Excerpt::$types[Excerpt::$length]) ){
return Excerpt::$types[Excerpt::$length];
} else {
return Excerpt::$length;
}
}
// Tells WP the new more
public static function new_more() {
if( isset(Excerpt::$more_types[Excerpt::$more]) ) {
return Excerpt::$more_types[Excerpt::$more];
} else {
$db = new ReadMore(Excerpt::$more);
return $db->readmore(Excerpt::$more);
}
}
// Tells WP the new strip tags
public static function new_tags() {
return Excerpt::$tags;
}
// Echoes out the excerpt
public static function output() {
return get_the_excerpt();
}
}
// An alias to the class
function get_kia_excerpt($length = 55, $more='[...]', $tags='') {
return Excerpt::filter($length, $more, $tags);
}
// An alias to the class
function kia_excerpt($length = 55, $more='[...]', $tags='') {
echo get_kia_excerpt($length, $more, $tags);
}
class ReadMore {
private $text;
public function __construct($text) {
$temp = "..." . '<a class="readmore" title="'. _('Permalink to').get_the_title() . '" href=" ' . get_permalink() . '">'._($text).'</a>';
$this->more = $temp;
}
public function readmore($text) {
return $this->more;
}
}
to then use this in thematic and swap out all the default excerpts for my kia_excerpt you'd do this:
//change default Thematic excerpts to kia_excerpts
function childtheme_excerpts($post) {
global $thematic_content_length;
if ( strtolower($thematic_content_length) == 'excerpt' ) {
$post = '';
$post .= get_kia_excerpt('short','ellipse','<strong><em><a>');
}
return $post;
}
add_filter('thematic_post', 'childtheme_excerpts');