WordPress excerpt to show 2 paragraphs

2019-07-25 13:30发布

I have created two categories within a WordPress site with one post in each category. I am pulling in and excerpt from each category post on different pages.

I am showing the post excerpt like so on each page and adding a read more link manually.

<?php query_posts('cat=4&showposts=1'); ?>
    <?php while (have_posts()) : the_post(); ?>
        <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
        <?php the_excerpt(); ?>
    <a href="<?php the_permalink(); ?>">Read More</a>
<?php endwhile; ?>

The following code will then end the excerpt after the first paragraph.

function post_single_paragrapgh($text, $raw_excerpt) {
    if( ! $raw_excerpt ) {
        $content = apply_filters( 'the_content', get_the_content() );
        $text = substr( $content, 0, strpos( $content, '</p>' ) + 4 );
    }
    return $text;
}

add_filter( 'wp_trim_excerpt', 'post_single_paragrapgh', 10, 2 );

What I would like to do is tell it to cut off after a second paragraph or, in fact an image and a paragraph.

It will pull in an image if there is one at the top of post but then I also want a further paragraph after the image or just two paragraphs of text. Either or.

I used this site for reference but method C1 throws up errors. https://www.bybe.net/wordpress-the_excerpt-show-first-paragraph/

Thanks in advance!

2条回答
The star\"
2楼-- · 2019-07-25 13:51

Are you open to a solution with regex? Look for <p>(Anything)</p> and merge the two first occurrence. The code is not tested but should work.

function post_single_paragrapgh($text, $raw_excerpt) {
    if( ! $raw_excerpt ) {
        $content = apply_filters( 'the_content', get_the_content() );

        preg_match_all( '~<p>(.*?)</p>~', $content, $matches );

        if( isset( $matches[0][0] ) && isset( $matches[0][1] ) ) {

            $text = $matches[0][0] . $matches[0][1];

        } else {

            $text = $matches[0][0];

        }
    }
    return $text;
}
查看更多
对你真心纯属浪费
3楼-- · 2019-07-25 14:02

I managed to figure out what I needed with this code, posted the question too hastily I guess.

function awesome_excerpt($awesomeness_excerpt) {

    global $post;
    $raw_excerpt = $awesomeness_excerpt;

    if ( '' == $awesomeness_excerpt ) {
        $awesomeness_excerpt = get_the_content('');
        $awesomeness_excerpt = strip_shortcodes( $awesomeness_excerpt );
        $awesomeness_excerpt = apply_filters('the_content', $awesomeness_excerpt);

        $awesomeness_excerpt = "<p>$awesomeness_excerpt</p>";
        $wanted_number_of_paragraph = 2;
        $tmp = explode ('</p>', $awesomeness_excerpt);
        for ($i = 0; $i < $wanted_number_of_paragraph; ++$i) {
            if (isset($tmp[$i]) && $tmp[$i] != '') {
                $tmp_to_add[$i] = $tmp[$i];
            }
        }

        $awesomeness_excerpt .= $excerpt_end;
        return $awesomeness_excerpt;
    }
    return apply_filters('awesome_excerpt', $awesomeness_excerpt, $raw_excerpt);
}

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

Just took out the end if at the bottom - must have been left there in error. Hopefully it can help others.

查看更多
登录 后发表回答