Skip to content

Instantly share code, notes, and snippets.

@chasereeves
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chasereeves/f48b56d5d649d7d94748 to your computer and use it in GitHub Desktop.
Save chasereeves/f48b56d5d649d7d94748 to your computer and use it in GitHub Desktop.
<?php
//=========================================================== Grab first paragraph (two if first is not long enough)
function get_first_two_paragraphs(){
global $post;
$str = wpautop( get_the_content() );
$psToGrab = 2; // number of paragraphs to get
$maxFirstParaLength = 200; // character max in first paragraph
$matches = array(); // matching elements will go here
$pattern = '%<p[^>]*>(.*?)</p>%i'; // match what's between all <p> tags
preg_match_all($pattern, $str, $matches); // do the regex match
$outparts = array_slice($matches[1], 0, $psToGrab); // chop off everything after the first 2 elements
$out = "";
foreach($outparts as $part) {
$toKeep = "<b><strong><em>";
$out .= strip_tags($part, $toKeep).' ';
if(strlen($out) > $maxFirstParaLength) {
break;
}
}
return $out;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment