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

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

1 条评论

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

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

留下评论