Skip to content

Instantly share code, notes, and snippets.

@forrestbe
Created May 30, 2016 02:20
Show Gist options
  • Select an option

  • Save forrestbe/5e55985961eb6ad18db7d9895cd121ca to your computer and use it in GitHub Desktop.

Select an option

Save forrestbe/5e55985961eb6ad18db7d9895cd121ca to your computer and use it in GitHub Desktop.

Revisions

  1. forrestbe created this gist May 30, 2016.
    18 changes: 18 additions & 0 deletions wp-get-excerpt.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    function get_excerpt_by_id($post_id, $length)
    {
    $the_post = get_post($post_id); //Gets post ID
    $the_excerpt = $the_post->post_content; //Gets post_content to be used as a basis for the excerpt
    $excerpt_length = $length; //Sets excerpt length by word count
    $the_excerpt = strip_tags(strip_shortcodes($the_excerpt)); //Strips tags and images
    $words = explode(' ', $the_excerpt, $excerpt_length + 1);

    if(count($words) > $excerpt_length) :
    array_pop($words);
    array_push($words, '');
    $the_excerpt = implode(' ', $words);
    endif;

    $the_excerpt = '<p>' . $the_excerpt . '</p>';

    return $the_excerpt;
    }