fix search results length and formatting

This commit is contained in:
Jonas Heinrich 2020-07-22 19:48:19 +02:00
parent dae32307ff
commit ef9644c8d7
2 changed files with 19 additions and 10 deletions

View file

@ -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', '<strong class="tx-indexedsearch-redMarkup">\0</strong>', $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', '<strong class="tx-indexedsearch-redMarkup">\0</strong>', $excerpt);
echo '<p>... ' . $excerpt . ' ...</p>';
}

View file

@ -77,7 +77,8 @@ get_header();
</span>
</h3>
<p class="tx-indexedsearch-description">
<?php the_excerpt(); ?>
<!-- <?php the_excerpt(); ?> -->
<?php search_excerpt_highlight(); ?>
</p>
<a href="#" class="link-more"><span>Weiterlesen</span></a>
</div>