From 2ae983108cceb22793278a00d2c65ec7cb2e86ed Mon Sep 17 00:00:00 2001 From: Keith Solomon Date: Fri, 9 Jan 2026 08:37:10 -0600 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8feature:=20Add=20custom=20excerpt=20fu?= =?UTF-8?q?nction?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/helpers.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/lib/helpers.php b/lib/helpers.php index ad1e75e..a1a5f75 100644 --- a/lib/helpers.php +++ b/lib/helpers.php @@ -164,6 +164,25 @@ function escEmbeds() { ); } +/** + * Generates a custom excerpt for the given text. + * + * @param string $text The text to generate the excerpt from. + * @param int $number_of_words The maximum number of words in the excerpt. Default is 55. + * @param string|null $more The string to append to the end of the excerpt if it is truncated. Default is null. + * @return void + */ +function customExcerpt( $text, $number_of_words = 55, $more = null ) { + $allowed_end = array( '.', '!', '?', '...' ); + $text_no_html = wp_strip_all_tags( $text ); + $trimmed_text = wp_trim_words( $text, $number_of_words, $more ); + $trimmed_text_length = strlen( $trimmed_text ); + $sentence_end_position = strposArray( $text_no_html, $allowed_end, $trimmed_text_length ); + $text_with_html = ( ( $sentence_end_position !== false ) ? substr( $text_no_html, 0, ( $sentence_end_position + 1 ) ) : $trimmed_text ); + + echo wp_kses_post( wpautop( $text_with_html ) ); +} + /** Print a variable to the console for debugging purposes. * * @param mixed $data The data to print to the console.