From ef9644c8d70f9229a69de5672b6732a8553f90a9 Mon Sep 17 00:00:00 2001 From: Jonas Heinrich Date: Wed, 22 Jul 2020 19:48:19 +0200 Subject: [PATCH] fix search results length and formatting --- kit/functions.php | 26 +++++++++++++++++--------- kit/search.php | 3 ++- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/kit/functions.php b/kit/functions.php index 592df0f..efc50aa 100644 --- a/kit/functions.php +++ b/kit/functions.php @@ -431,14 +431,22 @@ add_action( 'widgets_init', 'kit_widgets_init' ); # Highlight query in search results -function highlight_results($text) { - if (is_search() && !is_admin()) { - $sr = get_query_var('s'); - $keys = explode(' ', $sr); - $keys = array_filter($keys); - $text = preg_replace('/('.implode('|', $keys) .')/iu', '\0', $text); - } - return $text; +function limit_text($text, $limit) { + if (str_word_count($text, 0) > $limit) { + $words = str_word_count($text, 2); + $pos = array_keys($words); + $text = substr($text, 0, $pos[$limit]); + } + return $text; } -add_filter('the_excerpt', 'highlight_results'); +function search_excerpt_highlight() { + + $excerpt = wp_strip_all_tags( get_the_content() ); + $excerpt = limit_text( $excerpt, 32); + + $keys = implode('|', explode(' ', get_search_query())); + $excerpt = preg_replace('/(' . $keys .')/iu', '\0', $excerpt); + + echo '

... ' . $excerpt . ' ...

'; +} diff --git a/kit/search.php b/kit/search.php index d21438b..f015a13 100644 --- a/kit/search.php +++ b/kit/search.php @@ -77,7 +77,8 @@ get_header();

- + +

Weiterlesen