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; } 

1 Comment

  1. tzjh12345 says:

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

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

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

无觅相关文章插件,快速提升流量

任鸟飞网页设计博客 谜题推理 No.1 Web Design Gallery IndustryIdea LookForDesign AWebGallery Mobile Web Template iDesign Wallpaper Big Funny Picture iPhone Faves iPhone Mobi iPad Faves Android Faves BlackBerry Faves Best Design Magazine All Banner Templates Blog Earn Tips 是谁啊 只爱美人 家乡美 唯美爱 美人衣妆 AutoCAD 2D | Pro/E 3D江苏名企The HTML5 Templates