Wordpress excerpt by second paragraph

前端 未结 1 1913
闹比i
闹比i 2021-01-26 00:48

How to limit the excerpt length by paragraph, not word/char count? For example, excerpt shows only first two paragraphs, no matter how long the paragraphs are.

Thanks in

1条回答
  •  粉色の甜心
    2021-01-26 01:13

    Here is a function that keeps HTML tags in tact, adds a "Read More" link at the end of the excerpt and trims the excerpt after the first paragraph.

    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);
    // Here we choose how many paragraphs do we want to cutthe excerpt at, This part thanks to Clément Malet
    $wpse0001_excerpt = "

    $wpse0001_excerpt

    "; $wanted_number_of_paragraph = 2; $tmp = explode ('

    ', $wpse0001_excerpt); for ($i = 0; $i < $wanted_number_of_paragraph; ++$i) { if (isset($tmp[$i]) && $tmp[$i] != '') { $tmp_to_add[$i] = $tmp[$i]; } } $wpse0001_excerpt = implode('

    ', $tmp_to_add) . '

    '; $wpse0001_excerpt = str_replace(']]>', ']]>', $wpse0001_excerpt); $excerpt_end = ' ' . ' » ' . sprintf(__( 'Read more about: %s  »', 'pietergoosen' ), get_the_title()) . ''; $excerpt_more = apply_filters('excerpt_more', ' ' . $excerpt_end); //$pos = strrpos($wpse0001_excerpt, '

    EDIT

    Thanks to the help from @ClementMalet, I was able to tweak my function to make you choose the amount of paragraphs where you want to cut the excerpt. Please check his great answer here

    0 讨论(0)
提交回复
热议问题