Skip to content

Instantly share code, notes, and snippets.

@furenku
Last active September 8, 2016 06:35
Show Gist options
  • Select an option

  • Save furenku/10194a84f90bc5b9d4a8f832d898a31e to your computer and use it in GitHub Desktop.

Select an option

Save furenku/10194a84f90bc5b9d4a8f832d898a31e to your computer and use it in GitHub Desktop.
function search_excerpt_highlight() {
$excerpt = get_the_excerpt();
$keys = implode('|', explode(' ', get_search_query()));
$excerpt = preg_replace('/(' . $keys .')/iu', '<strong class="search-highlight">\0</strong>', $excerpt);
echo '<p>' . $excerpt . '</p>';
}
function search_title_highlight() {
$title = get_the_title();
$keys = implode('|', explode(' ', get_search_query()));
$title = preg_replace('/(' . $keys .')/iu', '<strong class="search-highlight">\0</strong>', $title);
echo $title;
}
.search-highlight {
color: #fa0;
}
<?php echo apply_filters('the_title', search_title_highlight() ); ?>
<?php echo apply_filters('the_excerpt', search_excerpt_highlight() ); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment