Created
January 21, 2014 19:21
-
-
Save bgallagh3r/8546465 to your computer and use it in GitHub Desktop.
Custom Excerpt class for Wordpress.
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
/* | |
|-------------------------------------------------------------------------- | |
| Custom Excerpt | |
|-------------------------------------------------------------------------- | |
*/ | |
class Excerpt { | |
// Default length (by WordPress) | |
public static $length = 55; | |
// So you can call: my_excerpt('short'); | |
public static $types = array( | |
'short' => 25, | |
'regular' => 55, | |
'long' => 100, | |
'promo'=>15 | |
); | |
public static $more = true; | |
/** | |
* 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 length($new_length = 55, $more = true) { | |
Excerpt::$length = $new_length; | |
Excerpt::$more = $more; | |
add_filter( 'excerpt_more', 'Excerpt::auto_excerpt_more' ); | |
add_filter('excerpt_length', 'Excerpt::new_length'); | |
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; | |
} | |
// Echoes out the excerpt | |
public static function output() { | |
the_excerpt(); | |
} | |
public static function continue_reading_link() { | |
return '<span class="readmore"><a href="'.get_permalink().'">Read More</a></span>'; | |
} | |
public static function auto_excerpt_more( ) { | |
if (Excerpt::$more) : | |
return ' …' . Excerpt::continue_reading_link(); | |
else : | |
return ' …'; | |
endif; | |
} | |
} | |
// An alias to the class | |
function excerpt($length = 55, $more=true) { | |
Excerpt::length($length, $more); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment