admin 发表于 2020.2.28 18:07

wordpress 搜索高亮提示

// 搜索高亮提示
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", "<span style=\"background-color:#ff0;\"><strong>$1</strong></span>", $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

页: [1]
查看完整版本: wordpress 搜索高亮提示