Last active
August 29, 2015 14:01
-
-
Save amitabhaghosh197/7e42794e166b992f6e3c to your computer and use it in GitHub Desktop.
Excerpt_class wordpress function
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
<?php | |
// Just copy the code in function.php | |
?> | |
<?php | |
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 | |
); | |
/** | |
* 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) { | |
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; | |
} | |
// Echoes out the excerpt | |
public static function output() { | |
the_excerpt(); | |
} | |
} | |
// An alias to the class | |
function custom_excerpt($length = 55) { | |
Excerpt::length($length); | |
} | |
//If you want More button in excerpt | |
//More Button Link in excerpt | |
function damiracle_custom_excerpt_more($more) { | |
global $post; | |
return '<a class="more-link" href="'. get_permalink($post->ID) . '">'. __('..More', 'damiracle') .'</a>'; | |
} | |
add_filter('excerpt_more', 'damiracle_custom_excerpt_more'); |
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
<?php | |
// Now Add wherever you require for excerpt | |
?> | |
<div> | |
<p> | |
<?php the_excerpt() ?> | |
</p> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment