I have written a some code to get custom excerpt_length
and excerpt_more
. The excerpt_length is working for both custom post type and general post. But I don't know why excerpt_more
is not working for custom post types. Below is my code.
class Excerpt {
// Default length (by WordPress)
public static $length = 55;
// So you can call: doctoplus_excerpt('short');
public static $types = array(
'ten'=>10,
'few' => 15,
'short' => 25,
'regular' => 55,
'long' => 100
);
//Default more
public static $more = '...';
//So you can call doctoplus_excerpt('short','Read More');
public static $moreTypes = array(
1 => 'Read More',
2 => 'More',
3 => 'View more'
);
/**
* Sets the length for the excerpt,
* then it adds the WP filter
* And automatically calls the_excerpt();
*
* @param string $new_length
* @return void
* @author Girija
*/
public static function length($new_length = 55) {
Excerpt::$length = $new_length;
add_filter('excerpt_length', 'Excerpt::new_length', 999);
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;
}
public static function more($new_more = 'Read More') {
Excerpt::$more = $new_more;
add_filter('excerpt_more', 'Excerpt::new_more', 999);
}
//Tell WP the custom more
public static function new_more() {
global $post;
if(isset(Excerpt::$moreTypes[Excerpt::$more]) ) {
return ' <a class="more-link" href="'. get_permalink($post->ID) . '">'. __(Excerpt::$moreTypes[Excerpt::$more], 'doctoplus') .'</a>';
} else {
return Excerpt::$more;
}
}
// Echoes out the excerpt
public static function output() {
the_excerpt();
}
}
// An alias to the class
function doctoplus_excerpt($length = 55, $more = "...") {
Excerpt::length($length);
Excerpt::more($more);
}
Can any one help me? I really need this to be done. Thanks in advance.
the_excerpt()
here. Hope that will helpdoctoplus_excerpt
.