将这两个函数添加到函数中。php
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;
}
编辑:要在搜索结果中使用\\u内容,请使用以下功能:
function search_content_highlight() {
$content = get_the_content();
$keys = implode(\'|\', explode(\' \', get_search_query()));
$content = preg_replace(\'/(\' . $keys .\')/iu\', \'<strong class="search-highlight">\\0</strong>\', $content);
echo \'<p>\' . $content . \'</p>\';
}
在您的循环或搜索中。php文件调用
<?php search_title_highlight(); ?>
而不是
<?php the_title(); ?>
和使用
<?php search_excerpt_highlight(); ?>
而不是
<?php the_excerpt(); ?>
在css中添加search highlight类,该类将以黄色突出显示所有搜索的单词。
.search-highlight {
background:#FFFF00
}