WordPress get the first paragraph of the body content to solve the problem that the summary is not displayed properly
Sometimes we use shortcodes in articles or pages, and when the archive page needs to display the summary, the shortcodes will be displayed together, which is certainly not what we want. At this time, it is a good option to display the first paragraph of the article as a summary.
function get_first_p($post)
{
// Extract the first paragraph by matching the p tag with a regular expression
if (preg_match('/(. *)/iU', trim(strip_tags($post->post_content, "")), $matches)) {
return $matches[1];
} else {
//Sometimes, the post may be segmented by soft returns \n for the segment marker
$post_content = explode("\n", trim(strip_tags($post->post_content)));
return $post_content ['0'];
}
}
Use this function directly where you need to display a summary.