Wordpress如何在搜索结果中高亮标记搜索关键字

在搜索结果中,常常需要高亮标记搜索关键字。以便区分搜索关键字与其他内容。

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
任鸟飞

任鸟飞网页设计网创办者 主要关注网页设计,网站建设,图形图像设计。 Twitter @renniaofei QQ:29900684

1 条评论

  1. tzjh12345 一月 17th, 2010   5:46 上午

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

留下评论

:wink: :-| :-x :twisted: :) 8-O :( :roll: :-P :oops: :-o :mrgreen: :lol: :idea: :-D :evil: :cry: 8) :arrow: :-? :?: :!: