Skip to content

Instantly share code, notes, and snippets.

@stalukder03
Forked from sammdec/custom_excerpt_wp.php
Created June 11, 2018 16:03
Show Gist options
  • Save stalukder03/b6bf06abc17eaec2cf21d8c43d45ac0a to your computer and use it in GitHub Desktop.
Save stalukder03/b6bf06abc17eaec2cf21d8c43d45ac0a 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