Skip to content

Instantly share code, notes, and snippets.

@brettkelly
Forked from banago/get-fisrt-paragraph.php
Last active December 25, 2015 17:59
Show Gist options
  • Save brettkelly/7017281 to your computer and use it in GitHub Desktop.
Save brettkelly/7017281 to your computer and use it in GitHub Desktop.
<?php
/**
* Get first paragraph from a WordPress post. Use inside the Loop.
*
* @return string
*/
function get_first_paragraph(){
global $post;
$str = wpautop( get_the_content() );
$firstP = strpos( $str, '</p>' );
$secondP = strpos( $str, '</p>', ($firstP + 1)
$p1 = substr( $str, 0, $firstP + 4 );
$p2 = substr( $str, ($firstP + 4), $secondP);
$p1 = strip_tags($p1, '<a><strong><em>');
$p2 = strip_tags($p2, '<a><strong><em>');
return '<p>'.$p1.'</p><p>'.$p2.'</p>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment