Como destacar os termos de pesquisa sem plugin
4 respostas
- votos
-
- 2011-05-01
Adicioneestas 2funções às suasfunções.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:
Para usar o_contentpara os resultados dapesquisa,use afunção abaixo:
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>'; }
no seu loop oubusca.php arquivo chamada
<?php search_title_highlight(); ?>
em vez de<?php the_title(); ?>
e use<?php search_excerpt_highlight(); ?>
em vez de<?php the_excerpt(); ?>
No seu CSS,adicione a classe depesquisa depesquisa que destacarátodas aspalavraspesquisadasem amarelo.
.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 }
-
Aplicar [``preg_quote () `] (http://php.net/preg_quote)para` $teclas 'paraimpedir que seu regexexpliqueem caso de caracteresespeciais,comoparênteses ou colchetes.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
-
Quetal destacar otermo depesquisa após o usuário clicano singlee vai dentro dopost?Em seguida,o **get_search_query () ** retorna uma string vaziaWhat 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
-
Aqueles devem serfiltrospara "the_excerpt"e "the_content".De qualquerforma:boa resposta,mas o comentário de @geertpoderia sertrabalhadoem :)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
-
Está substituindo otextono readmore hreftambém?Como consertaristo?it is replacing the text in the readmore href also? how to fix this?
- 1
- 2014-01-13
- Naveen
-
- 2014-12-09
O acimafuncionabem,euexecuto o código similar,mas amarre otítuloe otrechojuntos.Mas achou quebra quando alguémentraem umespaço "" ounoinício ounofinal de umtermo de consulta depesquisa.
assimive Adicionaresta linha:
$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');
Espero queisso seprojetepara ajudar os outros.
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
As soluções acima quebrar apágina se otermo depesquisa aparecer dentro dastags HTML.Você deve usar algo como:
$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);
-
obrigadomate vocêfez omeu dia :-)thanxs mate you made my day :-)
- 1
- 2016-01-26
- Agha Umair Ahmed
-
- 2020-03-02
combinado 2 respostaspara usarfiltros:
// 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');
Então,em seu CSS (porexemplo):
.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; }
Comoposso obter ostermos depesquisa destacados semplugin?