Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Wordpress Custom Excerpt function
function get_excerpt($count, $post_id){
$permalink = get_permalink($post_id);
$excerpt = get_post($post_id);
$excerpt = $excerpt->post_content;
$excerpt = strip_tags($excerpt);
$excerpt = substr($excerpt, 0, $count);
$excerpt = substr($excerpt, 0, strripos($excerpt, " "));
$excerpt = $excerpt;
// Want a read more link and ellipsis, remove the line above this and replace it with the one below.
$excerpt = $excerpt.'... <a href="'.$permalink.'">more</a>';
return $excerpt;
// When using loop first arg is no. of chars you want displayed second arg is passing the global post ID.
echo get_excerpt(100, $post_id);
// When using in custom get_posts loop
echo get_excerpt(100, $custom_foreach->ID);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment