在搜索结果中,常常需要高亮标记搜索关键字。以便区分搜索关键字与其他内容。
1. 修改search.php源代码。
1. 在<?php while (have_posts()) : the_post(); ?> <div <?php post_class() ?>>下面添加下面的代码。
<?php
$title = get_the_title();
$keys = explode(" ",$s);
$title = preg_replace('/('.implode('|', $keys) .')/iu',
'<strong class="search-excerpt">\0</strong>',
$title);
echo $title;
?>
2. 修改样式表style.css,添加下面的代码。
strong.search-excerpt { background: yellow; }
除非注明,本博客文章均为原创,转载请以链接形式标明本文地址
原创文章如转载,请注明:转载自 任鸟飞网页设计 [ http://www.renniaofei.com/ ]
本文链接地址: http://www.renniaofei.com/code/wordpress-highlight-searched-text-in-search-results/
引用通告地址 Trackback URL:直接在本文链接地址后添加 trackback/
本文短网址: http://bit.ly/9YhIP5
原创文章如转载,请注明:转载自 任鸟飞网页设计 [ http://www.renniaofei.com/ ]
本文链接地址: http://www.renniaofei.com/code/wordpress-highlight-searched-text-in-search-results/
引用通告地址 Trackback URL:直接在本文链接地址后添加 trackback/
本文短网址: http://bit.ly/9YhIP5




不错,很实用啊,收藏一下,有空用到时再来哦.