WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to extract nth paragraphs from content (4 posts)

  1. patv
    Member
    Posted 2 years ago #

    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];
  2. Roman
    Member
    Posted 2 years ago #

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

  3. patv
    Member
    Posted 2 years ago #

    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.

  4. patv
    Member
    Posted 2 years ago #

    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').

Topic Closed

This topic has been closed to new replies.

About this Topic