为了让 WordPress 搜索页面的搜索结果体验更佳,可以让 WordPress 搜索内容关键字高亮显示,方法如下:
「后台」→「外观」→「编辑」→ 「functions.php」文件,把下面的代码添加进去:
function highlight_search_keywords($text){
if ( is_search() ) {
$s = get_search_query();
$keys = explode(' ', $s);
$text = preg_replace('/(' . implode('|', $keys) . ')/iu', '<strong class="highlight">$1</strong>', $text);
}
return $text;
}
add_filter( 'the_title', 'highlight_search_keywords' );
add_filter( 'the_excerpt', 'highlight_search_keywords' );
© 版权声明
文章版权归作者所有,未经允许请勿转载。
相关文章
暂无评论...