feature: Add custom excerpt function

This commit is contained in:
Keith Solomon
2026-01-09 08:37:10 -06:00
parent c70aa6923a
commit 2ae983108c

View File

@@ -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.