For each post, I use the custom excerpt field to write a custom excerpt (180 characters max) for each of my posts, which I use as the meta description of my post for SEO.

When I show a list of my posts (archive pages, categories etc.) this excerpt is displayed as "teaser text" for each of my posts. The problem is that this text is too short, as it is written for meta description purposes.

Would it be possible to have a longer excerpt showing in category and index pages, yet keep my custom excerpt as a meta description of each post?

Btw, most of my posts have a specifically placed "read more" tag, which now is ignored as I have a custom excerpt.

  • Yes, you just call it in where you want it to display.
    – Aibrean
    Mar 8, 2016 at 14:45

2 Answers 2


We can try to filter the excerpt through the get_the_excerpt filter. By default, if we have a manual excerpt, the manual excerpt will be used and not the automatically created excerpt which is created by the wp_trim_excerpt() function.

We can alter this behavior. What we will do is, when we are inside the main query (in_the_loop()), we will return the output from the wp_trim_excerpt() function. This way, we keep all filters as per default. Whenever we are outside the main query, we can return the manually created excerpt, if it exists, otherwise the normal excerpt

add_filter( 'get_the_excerpt', function( $text )
    if ( in_the_loop() ) 
        return wp_trim_excerpt();

    return $text;
  • 1
    Tested and it works!
    – IXN
    Mar 8, 2016 at 22:11
  • Glad it worked. Enjoy ;-) Mar 9, 2016 at 4:36

This is a function that I use all the time to create a custom excerpt:

function custom_excerpt( $limit, $post_id=NULL ) 
    if ( $post_id == NULL ) { 
        $the_excerpt = get_the_excerpt(); 
    } else { 
        $the_excerpt = get_the_excerpt($post_id); 

    $excerpt = explode( ' ', $the_excerpt, $limit );     

    if ( count( $excerpt ) >= $limit ) {
        $excerpt = implode( " ",$excerpt ) . '...';
    } else {
        $excerpt = implode( " ",$excerpt );
    $excerpt = preg_replace( '`\[[^\]]*\]`', '', $excerpt );

return $excerpt;

Then you would use it like so in your theme templates where you want to use your custom excerpt length

echo custom_excerpt(50,1);

or without defining the post id

echo custom_excerpt(50,NULL);

Where the first number (50) is the length of the excerpt and the second number (1) is the post id.

  • Thanks. However, for each post, I have written an excerpt in the custom excerpt field (for seo purposes). Yet, I want to show a different, longer excerpt on the archive, category etc. pages. Now the get_the_excerpt function calls the text I have written in the custom excerpt field.
    – IXN
    Mar 8, 2016 at 17:14
  • Ah so you acre asking how to increase the max excerpt length.
    – thebigtine
    Mar 8, 2016 at 17:16
  • Keep the text in the excerpt field for seo purposes and show a different (probably the automatically generated) excerpt in the category pages.
    – IXN
    Mar 8, 2016 at 17:25
  • You should really work on the neatness of your code. I have cleaned it up for better readability. It is difficult to read code that is that messy Mar 8, 2016 at 17:49
  • Well looks like our definition of unclean code was just me saving space.
    – thebigtine
    Mar 8, 2016 at 17:55

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.