【发布时间】:2016-05-03 11:21:30
【问题描述】:
我想在 WordPress 的内容结果中突出显示搜索词。
我尝试了一些标题、摘录和内容突出显示的功能。标题和摘录工作正常,但内容不正常。它扰乱了我的内容布局。
在使用功能突出显示内容中的搜索词之后。看起来是这样的
我使用标题高亮的功能是
function search_title() {
$title = get_the_title();
$keys = implode('|', explode(' ', get_search_query()));
$content = strip_tags($content);
$title = preg_replace('/(' . $keys .')/iu', '<strong class="search-highlight">\0</strong>', $title);
echo $title;
}
我用于内容的功能是
function search_content() {
$content = get_the_content();
$keys = implode('|', explode(' ', get_search_query()));
// $content = strip_tags($content);
$content = preg_replace('/(' . $keys .')/iu', '<strong class="search-highlight">\0</strong>', $content);
$content = preg_replace('~(?:\[/?)[^/\]]+/?\]~s', '', $content);
echo '<p>' . $content . '</p>';
}
它工作正常,但破坏了我的布局。
我也尝试了一些 jquery 方法。但没有运气。
【问题讨论】:
-
有关 jQuery 解决方案,请查看 mark.js!
标签: php jquery regex wordpress