如何在没有插件的情况下突出显示搜索词
4 个回答
- 投票数
-
- 2011-05-01
将这两个函数添加到您的functions.php
function search_excerpt_highlight() { $excerpt = get_the_excerpt(); $keys = implode('|', explode(' ', get_search_query())); $excerpt = preg_replace('/(' . $keys .')/iu', '<strong class="search-highlight">\0</strong>', $excerpt); echo '<p>' . $excerpt . '</p>'; } function search_title_highlight() { $title = get_the_title(); $keys = implode('|', explode(' ', get_search_query())); $title = preg_replace('/(' . $keys .')/iu', '<strong class="search-highlight">\0</strong>', $title); echo $title; }
编辑:
要在搜索结果中使用the_content,请使用以下功能:
function search_content_highlight() { $content = get_the_content(); $keys = implode('|', explode(' ', get_search_query())); $content = preg_replace('/(' . $keys .')/iu', '<strong class="search-highlight">\0</strong>', $content); echo '<p>' . $content . '</p>'; }
在循环或search.php文件中调用
<?php search_title_highlight(); ?>
而不是<?php the_title(); ?>
并使用<?php search_excerpt_highlight(); ?>
而不是<?php the_excerpt(); ?>
在CSS中添加搜索突出显示类,该类将以黄色突出显示所有搜索到的单词.
.search-highlight { background:#FFFF00 }
Add these 2 functions to your functions.php
function search_excerpt_highlight() { $excerpt = get_the_excerpt(); $keys = implode('|', explode(' ', get_search_query())); $excerpt = preg_replace('/(' . $keys .')/iu', '<strong class="search-highlight">\0</strong>', $excerpt); echo '<p>' . $excerpt . '</p>'; } function search_title_highlight() { $title = get_the_title(); $keys = implode('|', explode(' ', get_search_query())); $title = preg_replace('/(' . $keys .')/iu', '<strong class="search-highlight">\0</strong>', $title); echo $title; }
Edit:
To use the_content for your search results use the function below:
function search_content_highlight() { $content = get_the_content(); $keys = implode('|', explode(' ', get_search_query())); $content = preg_replace('/(' . $keys .')/iu', '<strong class="search-highlight">\0</strong>', $content); echo '<p>' . $content . '</p>'; }
In your loop or search.php file call
<?php search_title_highlight(); ?>
instead of<?php the_title(); ?>
and use<?php search_excerpt_highlight(); ?>
instead of<?php the_excerpt(); ?>
In your css add the search-highlight class which will highlight all searched words in yellow.
.search-highlight { background:#FFFF00 }
-
将[`preg_quote()`](http://php.net/preg_quote)应用于`$ keys`,以防止正则表达式因括号或方括号等特殊字符而破裂.Apply [`preg_quote()`](http://php.net/preg_quote) to `$keys` to prevent your regex from blowing up in case of special characters like parentheses or brackets.
- 4
- 2011-05-01
- Geert
-
在用户单击单曲并进入帖子后,突出显示搜索词该怎么办?然后**get_search_query()**返回一个空字符串What about highlighting the search term after the user clicks on the single and goes inside the post? Then the **get_search_query()** returns an empty string
- 1
- 2011-05-22
- Maor Barazany
-
这些应该是`the_excerpt`和`the_content`的过滤器.无论如何:好的答案,但是@Geert的评论可以在:)中使用.Those should be filters for `the_excerpt` and `the_content` instead. Anyway: Nice answer, but the comment from @Geert could be worked in :)
- 1
- 2012-10-13
- kaiser
-
是否也要替换readmore href中的文本?如何解决这个问题?it is replacing the text in the readmore href also? how to fix this?
- 1
- 2014-01-13
- Naveen
-
- 2014-12-09
上面的代码运行得很好,我运行了类似的代码,但是将标题和摘录绑定在一起.但是发现当有人在搜索查询词的开头或结尾输入空格""时,它就会中断.
所以我添加以下行:
$keys = array_filter($keys);
// Add Bold to searched term function highlight_results($text){ if(is_search() && !is_admin()){ $sr = get_query_var('s'); $keys = explode(" ",$sr); $keys = array_filter($keys); $text = preg_replace('/('.implode('|', $keys) .')/iu', ''.$sr.'', $text); } return $text; } add_filter('the_excerpt', 'highlight_results'); add_filter('the_title', 'highlight_results');
希望这可以帮助别人.
The above works well I've run the similar code, but tie the title and excerpt together. But found it breaks when someone enters a space " " either at the beginning or end of a search query term.
So Ive add this line:
$keys = array_filter($keys);
// Add Bold to searched term function highlight_results($text){ if(is_search() && !is_admin()){ $sr = get_query_var('s'); $keys = explode(" ",$sr); $keys = array_filter($keys); $text = preg_replace('/('.implode('|', $keys) .')/iu', ''.$sr.'', $text); } return $text; } add_filter('the_excerpt', 'highlight_results'); add_filter('the_title', 'highlight_results');
Hope this proves to help others.
-
- 2015-07-27
如果搜索词出现在HTML标记内,则上述解决方案会中断页面.您应该使用类似这样的内容:
$regEx = '\'(?!((<.*?)|(<a.*?)))(\b'. implode('|', $keys) . '\b)(?!(([^<>]*?)>)|([^>]*?</a>))\'iu'; $text = preg_replace($regEx, '<strong class="search-highlight">\0</strong>', $text);
The above solutions break the page if the search term appears inside HTML tags. You should use something like:
$regEx = '\'(?!((<.*?)|(<a.*?)))(\b'. implode('|', $keys) . '\b)(?!(([^<>]*?)>)|([^>]*?</a>))\'iu'; $text = preg_replace($regEx, '<strong class="search-highlight">\0</strong>', $text);
-
谢谢你,你让我开心了:-)thanxs mate you made my day :-)
- 1
- 2016-01-26
- Agha Umair Ahmed
-
- 2020-03-02
结合使用过滤器的2个答案:
// Add class to searched terms function highlight_results($text) { if (is_search() && !is_admin()) { $sr = get_query_var('s'); $keys = explode(' ', $sr); $keys = array_filter($keys); $text = preg_replace('/('.implode('|', $keys) .')/iu', '<span class="search-highlight">\0</span>', $text); } return $text; } add_filter('the_excerpt', 'highlight_results'); add_filter('the_title', 'highlight_results');
然后在您的CSS中(例如):
.search-highlight { background: #ffff94; padding: 0 2px; }
Combined 2 answers to use filters :
// Add class to searched terms function highlight_results($text) { if (is_search() && !is_admin()) { $sr = get_query_var('s'); $keys = explode(' ', $sr); $keys = array_filter($keys); $text = preg_replace('/('.implode('|', $keys) .')/iu', '<span class="search-highlight">\0</span>', $text); } return $text; } add_filter('the_excerpt', 'highlight_results'); add_filter('the_title', 'highlight_results');
Then in your CSS (for example) :
.search-highlight { background: #ffff94; padding: 0 2px; }
如何在没有插件的情况下突出显示搜索词?