WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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>
share|improve this question

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.

share|improve this answer

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.

share|improve this answer

You can use the built in function

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


share|improve this answer
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 '15 at 12:41

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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