WordPress搜索页面关键词加亮

网站APP, 转自点点 12 年前 回复

, ,

function search_word_replace($buffer){
    if(is_search()){
        $arr = explode(" ", get_search_query());
        $arr = array_unique($arr);
        foreach($arr as $v)
            if($v)
                $buffer = preg_replace("/(".$v.")/i", "$1", $buffer);
    }
    return $buffer;
}
add_filter("the_title", "search_word_replace", 200);
add_filter("the_excerpt", "search_word_replace", 200);
add_filter("the_content", "search_word_replace", 200);

加载主题文件夹下的functions.php中,嵌套em标签,可以根据需要调整标签样式。Via: http://www.cmhello.com/wordpress-search-keyword-highlight.html

支付宝打赏微信打赏

如果此文对你有帮助,欢迎打赏作者。


Deprecated: str_contains(): Passing null to parameter #1 ($haystack) of type string is deprecated in /home/wwwroot/hilau.com/wp-includes/comment-template.php on line 2684

发表评论

欢迎回来 (打开)

(必填)