Skip to content

Instantly share code, notes, and snippets.

@simonpioli
Last active December 18, 2015 19:29
Show Gist options
  • Save simonpioli/5833466 to your computer and use it in GitHub Desktop.
Save simonpioli/5833466 to your computer and use it in GitHub Desktop.
Allows you to specify the excerpt length per page rather than through a global setting in your functions.php.
<?php
/** In functions.php **/
/**
* Customise Excerpt Length
* @param int $new_length Length of excerpt
* @param mixed $new_more String to output at the end of the excerpt, if false reverts to default
* @return type
*/
function custom_excerpt($new_length = 20, $new_more = false) {
add_filter('excerpt_length', function () use ($new_length) {
return $new_length;
}, 999);
if($new_more) {
add_filter('excerpt_more', function () use ($new_more) {
return $new_more;
});
}
$output = get_the_excerpt();
$output = apply_filters('wptexturize', $output);
$output = apply_filters('convert_chars', $output);
$output = '<p>' . $output . '</p>';
echo $output;
}
/**
* Customise String at End of Excerpt
* @param dunno $more
* @return string
*/
function new_excerpt_more($more) {
global $post;
return ' <a class="more_link" href="'. get_permalink($post->ID) . '">Read More</a>';
}
add_filter('excerpt_more', 'new_excerpt_more');
/** Template Example **/
custom_excerpt(20);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment