endif;
echo "<div class='blog-excerpt-title'><h4>$post->post_title</h4></div>";
echo "<div class='blog-excerpt-image-container'><img src='$image[0]'/></div>";
- echo "<div class='blog-excerpt-content'>". content_excerpt($post->post_content, 30)."</div>";
+ echo "<div class='blog-excerpt-content'>". content_excerpt($post->ID, 30)."</div>";
}
$output = ob_get_contents();
ob_end_clean();
return $output;
}
-/* Returns the first $wordsreturned out of $string. If string
-contains fewer words than $wordsreturned, the entire string
-is returned.
-*/
-function content_excerpt($string, $wordsreturned){
- $excerpt = $string; // Just in case of a problem
-
- $array = explode(" ", $string);
- /* Already short enough, return the whole thing
- */
- if (count($array)<=$wordsreturned){
- $excerpt = $string;
- }else{
- array_splice($array, $wordsreturned);
- $excerpt = implode(" ", $array)." ...";
- }
- return $excerpt;
+function content_excerpt($post_id, $count = 50){
+ $the_post = get_post($post_id); //Gets post ID
+ $the_excerpt = ($the_post ? $the_post->post_content : null); //Gets post_content to be used as a basis for the excerpt
+ $excerpt_length = $count; //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;
+
+ return $the_excerpt;
}
\ No newline at end of file