Trim Manual Excerpts

I am using Cornerstone to add content to the posts in my blog. Because of this, the excerpts are not automatically generated - they are empty in the category or archive pages. I am using manual excerpts to achieve this, but I can’t set a default word or character limit.

Is there a way of limiting the length of the manual excerpt by adding a snippet of code to my child’s theme function.php?

Hello @DiegoMantilla,

Thanks for writing in! You are already adding the excerpts manually. You have to limit the word count while you were inserting the excerpt. The excerpt length being set by WordPress or in the Theme Options is for auto excerpts only. With manual excerpt, you have the control to it.

Hope this helps.

Hi RueNel,
I get it. But I would like to just copy and paste the first paragraph of the post into the manual excerpt field, without having to count for characters, and have a script in functions.php that trims it to a set number of chararcters. It just simplifies the process of adding content to the blog. Can you point me the in right direction to achieve this?

Hi @DiegoMantilla,

This article would help http://bacsoftwareconsulting.com/blog/index.php/wordpress-cat/automatically-control-manual-and-auto-generated-excerpt-in-wordpress/. It will apply the excerpt trimming even on manual excerpt.

And this too https://wpquestions.com/How_do_I_trim_the_excerpt_to_a_certain_character_count/716

Thanks!

Hi Rad,
Thanks for your reply. I was able to make this work (following your suggestion). So here it is, for user trying to achieve the same thing:

   // Manual Excerpt Auto-Trim
// =============================================================================

function bac_manual_auto_excerpt($text) {
    global $post;
    $raw_excerpt = $text;
    if ( '' == $text ) {
        $text = get_the_content('');
        $text = strip_shortcodes( $text );
        $text = apply_filters('the_content', $text);
        $text = str_replace(']]>', ']]>', $text);
    }    
    $text = strip_tags($text);
    /*** Change the excerpt words length. If you like. ***/
    $excerpt_length = apply_filters('excerpt_length', 30);
     
    /*** Change the Excerpt ending. If you like. ***/
    $excerpt_end = ' <a href="'. get_permalink($post->ID) . '">' . '&raquo; Continue Reading.' . '</a>'; 
    $excerpt_more = apply_filters('excerpt_more', ' ' . $excerpt_end);
     
    $text = wp_trim_words( $text, $excerpt_length, $excerpt_more ); 
    return apply_filters('wp_trim_excerpt', $text, $raw_excerpt); 
}
add_filter('get_the_excerpt', 'bac_manual_auto_excerpt', 5);

If you check the code there is a line that is setting the excerpt lenght to 30, It doesn’t work by itself. You will have to go to Theme Options / Blog and change the excerpt length. That will adjust the length of the manual excerpt on the category and archive pages for the blog.

Hope this helps.

Glad to hear you got it sorted, Diego. Thanks for sharing the code.

Cheers!

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.