0

I have successfully implemented a solution from WPSE that trims the except to the first paragraph, but I'd like to change that to trim after the second paragraph instead.

There's a link floating around as well that supposedly allows you to access the paragraphs as an array but that link is now dead.

Suggestions for how to modify the linked solution, or perhaps a new solution?

0

I have modified the earlier solution to your needs.

You need to change following line

$wpse0001_excerpt = substr( $wpse0001_excerpt, 0, strpos( $wpse0001_excerpt, '</p>' ) + 4 );

to this

$wpse0001_excerpt = explode('</p>', $wpse0001_excerpt, 3);

// unset the rest of the contents
if(isset($wpse0001_excerpt[3]))         unset($wpse0001_excerpt[3]);            
// append </p> to the end of last paragraph
if(isset($wpse0001_excerpt[2]))          $wpse0001_excerpt[1] .= '</p>';
// combine all the paragraphs with appending the ending tag.
$wpse0001_excerpt = implode('</p>', $wpse0001_excerpt);

Reproducing the whole solution here.

<?php

if ( ! function_exists( 'wpse0001_custom_wp_trim_excerpt' ) ) : 

    function wpse0001_custom_wp_trim_excerpt($wpse0001_excerpt) {
    global $post;
    $raw_excerpt = $wpse0001_excerpt;
        if ( '' == $wpse0001_excerpt ) {

            $wpse0001_excerpt = get_the_content('');
            $wpse0001_excerpt = strip_shortcodes( $wpse0001_excerpt );
            $wpse0001_excerpt = apply_filters('the_content', $wpse0001_excerpt);

            $wpse0001_excerpt = explode('</p>', $wpse0001_excerpt, 3);

            // unset the rest of the contents
            if(isset($wpse0001_excerpt[3]))         unset($wpse0001_excerpt[3]);            
            // append </p> to the end of last paragraph
            if(isset($wpse0001_excerpt[2]))          $wpse0001_excerpt[1] .= '</p>';
            // combine all the paragraphs with appending the ending tag.
            $wpse0001_excerpt = implode('</p>', $wpse0001_excerpt);

            // $wpse0001_excerpt = substr( $wpse0001_excerpt, 0, strpos( $wpse0001_excerpt, '</p>', strpos( $wpse0001_excerpt, '</p>' ) + 4 ) + 4);

            $wpse0001_excerpt = str_replace(']]>', ']]&gt;', $wpse0001_excerpt);

            $excerpt_end = ' <a href="'. esc_url( get_permalink() ) . '">' . '&nbsp;&raquo;&nbsp;' . sprintf(__( 'Read more about: %s &nbsp;&raquo;', 'pietergoosen' ), get_the_title()) . '</a>'; 
            $excerpt_more = apply_filters('excerpt_more', ' ' . $excerpt_end); 

            //$pos = strrpos($wpse0001_excerpt, '</');
            //if ($pos !== false)
            // Inside last HTML tag
            //$wpse0001_excerpt = substr_replace($wpse0001_excerpt, $excerpt_end, $pos, 0);
            //else
            // After the content
            $wpse0001_excerpt .= $excerpt_end;

            return $wpse0001_excerpt;   

        }
        return apply_filters('wpse0001_custom_wp_trim_excerpt', $wpse0001_excerpt, $raw_excerpt);
    }

endif; 

remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'wpse0001_custom_wp_trim_excerpt'); 

Note: Its not been tested.

| improve this answer | |
0

To be really honest here, there is no method in determining a paragraph. Yes, Wordpress has a filter called wpautop that set p tags (paragraphs) based on double line breaks, and that is all that Wordpress defines as a paragraph, a piece of text separated by a double line break from another piece of text.

enter image description here

Lets look at the screenshot from the HTML from a test post on my dev site and lets analyse that (My dev site is in Afrikaans, so excuse the text)

Here is what a paragraph can be

  • ARROW 1 This is a empty paragraph inserted by the code in the other answer. This counts as a full paragraph as it has an opening and closing p tag

  • ARROW 2 Movies, audio files, images and anything added in an a tag which is separated by double line breaks from the rest of the content

  • ARROW 5 A paragraph can simply be just an empty space. If a double empty line break falls between two line breaks, that is considered as a paragraph

  • ARROW 6 One single word like the word Ingredients or Method on a recipe website that is sometimes in between two double line breaks. Even just one single sentence can be a paragraph

Here is what is not a paragraph

  • ARROW 3 & 4 Although paragraphs are defined as double line breaks, some HTML tags are excluded and aren't wrapped in p tags, regardless whether they are in between double line breaks or not. What immediately comes to mind are h, li and ul tags.

CONCLUSION

You will have to come up with a plan and set way on what are you going to count as paragraphs and what not. Simply counting p tags will definitely fail somewhere. This is really something that can get complex and a real mess.

I did at some stage post code to this, but have deleted it as that is what happened, it became a mess with accurate counting of paragraphs. My solution would be, look at your structures, sit down and devise a plan and workflow on how you are going to count paragraphps and what are you going to do with tags like h, li and ul tags

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.