• Resolved patv

    (@patv)


    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];
Viewing 3 replies - 1 through 3 (of 3 total)
  • Roman

    (@polshikovrm)

    Wow … And what you do not like the function the_excerpt () and enter text in the quote *?

    Thread Starter patv

    (@patv)

    I’m using the_excerpt for another function – and I don’t want to assume the client will copy over the exact same content into the excerpt while I’m quite literally extracting from the post’s content.

    Thread Starter patv

    (@patv)

    I’ve worked it out… WordPress’ linebreaks can be detected using ‘\n\r\n’. This makes for a clean segmentation of the paragraphs using get_the_content (if not filtered using ‘the_content’).

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to extract nth paragraphs from content’ is closed to new replies.