- method 1 uses "get_the_excerpt()"
- method 2 uses "get_the_content()"
Last active
October 7, 2016 18:03
-
-
Save ccurtin/ffad6a3a11fb3f5982b4 to your computer and use it in GitHub Desktop.
Wordpress: Custom Excerpt Length Function
This file contains 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 | |
/** | |
* Custom Excerpt length for Wordpress. | |
* @author LordAzriel | |
* @link https://wordpress.org/support/topic/two-different-excerpt-lengths | |
* @param integer $new_length [Enter the number of words to show] | |
* @param string $new_more [Enter the trailing text after the last word] | |
* @return string [returns the excerpt with new word count] | |
*/ | |
function excerpt($new_length = 60, $new_more = '...') { | |
add_filter('excerpt_length', function () use ($new_length) { | |
return $new_length; | |
}, 999); | |
add_filter('excerpt_more', function () use ($new_more) { | |
return $new_more; | |
}); | |
$output = get_the_excerpt(); | |
$output = apply_filters('wptexturize', apply_filters('convert_chars',$output)); | |
$output = '<p>' . $output . '</p>'; | |
return $output; | |
} | |
?> | |
<!-- Can use in template file like so --> | |
<div class="latest-blog-copy"> | |
<p><?php echo excerpt(95,"...") ?></p> | |
</div> | |
<!-- Alternatively and more elegantly would be to use the follow. | |
#note to self: check to see if the_content() is a more intensive db request comapred to the_excerpt() | |
--> | |
<div class="latest-blog-copy"> | |
<p><?php echo wp_trim_words( get_the_content(), 60 )?></p> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment