Customizing get_the_excerpt() to specific length and “Read More” output.

I am costuming a template. There is a list grabbing the introduction from the first 1-2 paragraphs (all the articles from a category). If I set the excerpt to 295 words, sometimes the list grabs additional words from the next paragraph. I would like to add a Read More tag to stop it. Can someone help me with that part?

<div id="all-div-cabrand-content-stories">
    <div class="kids-families-con-cabrand-stories">
        <?php echo get_the_post_thumbnail($page->ID, 'thubmnailstorysmall'); ?>
    </div>
    <div class="kids-con-cabrand-new-stories">
        <span>
            <?php print substr(get_the_excerpt(),wp_trim_excerpt(),295); ?>
            <i><a style="color:#1975D1;float:Right;" class="title" href="<?php the_permalink() ?>" rel="bookmark">Click for Story & Video</a></i>
            <br/>
        </span>
    </div>
</div>

Related posts

Leave a Reply

5 comments

  1. To get a specific length you can use: wp_trim_words function. It has 3 parameters.

    1. Text to trim. Ex: get_the_content()
    2. Number of words. Ex: 295
    3. What to append after end of the text. Ex: '' This means null.

    Use this:

    <span>
        <?php echo wp_trim_words( get_the_content(), 295, '' ); ?>
        <i><a style="color:#1975D1;float:Right;" class="title" href="<?php
            the_permalink() ?>" rel="bookmark">Click for Story & Video</a></i>
        <br/>
    </span>
    
  2. You can grab the first one or two paragraphs with a regular expression (regexp)

    function custom_excerpt( $content = '' ){
    
        if( empty( $content ) )
            return $content;
    
        $result = '';
        $matches = array();
    
        // grab all paragraphs from $content
        preg_match_all( '#<s*p[^>]*>(.*?)<s*/s*p>#ui', $content, $matches );
    
        if( ! empty( $matches ) ){
    
            // add the first paragraph
            $result = $matches[0][0];
    
            // add the swecond paragraph if available
            if( isset( $matches[0][1] ) )
                $result .= $matches[0][1];
    
            // set the excerpt length
            add_filter( 'excerpt_length', 'custom_excerpt_length' );
    
            // create the custom excerpt
            $result = custom_trim_excerpt( $result );
    
        }
    
        return $result;
    
    }
    
    function custom_excerpt_length(){
    
        return 295;
    
     }
    
    function custom_trim_excerpt( $text = '' ){
    
        $text = strip_shortcodes( $text );
    
        $text = apply_filters('the_content', $text);
        $text = str_replace(']]>', ']]&gt;', $text);
        $excerpt_length = apply_filters('excerpt_length', 55);
        $excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
        $text = wp_trim_words( $text, $excerpt_length, $excerpt_more );
    
        return $text;
    }
    

    Call the function with

    <?php print custom_excerpt( get_the_content( 'Read More' ) ); ?>
    

    This is a bit tricky because you can not hand over wp_trim_excerpt() a text. wp_trim_excerpt() will simply return the text if one is given.
    You have to copy and customize the function a bit.

  3. To get what you want you need to do two things.

    1) Establish a custom excerpt length (in words, not characters), best achieved by following this answer.

    2) Just call wp_trim_excerpt(), don’t wrap it inside of substr

    Your line of code above is not doing what you are expecting it to do. I believe it’s returning the first 295 characters of the excerpt, but I’m not fully sure of what the php subtr() function is going to do when you pass it a string as the second argument when it’s expecting an integer.