Skip to content

Instantly share code, notes, and snippets.

@sammdec
Last active August 15, 2018 12:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sammdec/4050714 to your computer and use it in GitHub Desktop.
Save sammdec/4050714 to your computer and use it in GitHub Desktop.
Wordpress Custom Excerpt function
<?php
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;
}
?>
<?php
// 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