Auto-Generate Excerpt from Post Content
Generates a trimmed excerpt from the post body when no manual excerpt has been written.
PHPby SnipCraft
php
<?php
add_filter( 'the_excerpt', 'scseed_auto_generate_excerpt' );
function scseed_auto_generate_excerpt( $excerpt ) {
global $post;
if ( ! empty( trim( $excerpt ) ) || empty( $post ) ) {
return $excerpt;
}
$scseed_word_limit = 30; // Adjust word count as needed
$content = get_post_field( 'post_content', $post->ID );
$content = strip_shortcodes( $content );
$content = wp_strip_all_tags( $content );
$content = preg_replace( '/s+/', ' ', trim( $content ) );
$words = explode( ' ', $content );
if ( count( $words ) <= $scseed_word_limit ) {
return $content;
}
return implode( ' ', array_slice( $words, 0, $scseed_word_limit ) ) . '…';
}#auto#content#display#excerpt#post