How to extract nth paragraphs from content
-
I have a template file that I want to extract the first two paragraphs from the the post’s content in order to display them in a very specific way (side-by side columns, and i need to apply some specific styles & classes to the first paragraph).
I’ve tried to write a code that would explode my get_the_content on the line-breaks and then every item in my array should be a paragraph. I can break my content into separate paragraphs, but there line-break values stored in my array between paragraphs (presumably marking the line-breaks). I can code a check that would get the first and second paragraph that are not these ‘line-break’ values, but i don’t know what these values are in order to compare them.
Maybe my logic isn’t the best – does someone know how to complete what i’m trying to complete? Basically be able to extract the nth paragraph in my post content.
In case, here is my code:
//$offset : the nth paragraph i want to extract $content = get_the_content(); $content = explode("\r", $content); $j = 0; for($i = 0; $i < sizeof($content) && $i <= $offset; $i++){ if(!empty($content[$i])){ $j++; } } echo $content[$j];
- The topic ‘How to extract nth paragraphs from content’ is closed to new replies.