我们使用Wordpress建好了网站,可以在网站上添加搜索功能。制作的wordpress 网站搜索功能后,搜索结果的颜色都是一样的,我们可以对搜索结果进行改造,实现WordPress搜索结果关键词飘红。WordPress网站想要在搜索结果时实现关键词飘红效果的方法是什么?一起了解一下。
方法/步骤:
打开自己网站正在使用的Wordpress模板,找到搜索结果模板search.php;
使用DW软件打开search.php,找到循环代码,类似下面这样的:
在循环代码下面,添加以下的代码:
<?php
$s = trim(get_search_query()) ? trim(get_search_query()) : 0;
$title = get_the_title();
//300是摘要字符数,……是结束符号。
$content = mb_strimwidth(strip_tags(apply_filters(‘the_content’, $post->post_content)), 0, 300,“……”);
if($s){$keys = explode(” “,$s);
$title = preg_replace(‘/(‘.implode(‘|’, $keys) .‘)/iu’,‘<strong style=”color: #953b39;”>
$s = trim(get_search_query()) ? trim(get_search_query()) : 0;
$title = get_the_title();
//300是摘要字符数,……是结束符号。
$content = mb_strimwidth(strip_tags(apply_filters(‘the_content’, $post->post_content)), 0, 300,“……”);
if($s){$keys = explode(” “,$s);
$title = preg_replace(‘/(‘.implode(‘|’, $keys) .‘)/iu’,‘<strong style=”color: #953b39;”>