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 class="kids-con-cabrand-new-stories">
            <?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>

5 Answers 5


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:

    <?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>
  • 1
    Explaining what your code does and how it answers the question would improve your answer.
    – Gabriel
    Aug 6, 2016 at 19:58

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.


You can use this function:

function get_excerpt_trim($num_words='20', $more='...'){
    $excerpt = get_the_excerpt();
    $excerpt = wp_trim_words( $excerpt, $num_words , $more );
    return $excerpt;


  • Nice one, works perfect Feb 18, 2021 at 15:51

You can use the built in function

function custom_excerpt_length( $length ) {
    return 20;
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );


  • 1
    Welcome to WPSE Vee. If you read the question again you might notice that this isn't really an answer to the question asked. But anyway thanks for trying and once again welcome to WPSE!
    – kraftner
    Mar 16, 2015 at 12:41

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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