Come evidenziare i termini di ricerca senza plugin
4 risposta
- voti
-
- 2011-05-01
Aggiungi queste 2funzioni altuofunctions.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; }
Modifica:
Per utilizzarethe_contentperi risultati di ricerca,utilizza lafunzione seguente:
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>'; }
Neltuo loop onelfile search.php chiama
<?php search_title_highlight(); ?>
invece di<?php the_title(); ?>
e utilizza<?php search_excerpt_highlight(); ?>
invece di<?php the_excerpt(); ?>
Neltuo css aggiungi la classe dievidenziazione della ricerca cheevidenzieràtutte leparole cercatein giallo.
.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 }
-
Applica [`preg_quote ()`] (http://php.net/preg_quote) a `$ keys`perevitare che latua regexesplodain caso di caratteri speciali comeparentesi oparentesi.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
-
Chene dici dievidenziareiltermine di ricerca dopo che l'utente ha cliccato sul singoloed èentratonelpost?Quindi **get_search_query () ** restituisce una stringa vuotaWhat 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
-
Questi dovrebberoinveceesserefiltriper "the_excerpt"e "the_content".Comunque:bella risposta,mail commento di @Geertpotrebbeessereelaborato :)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
-
sta sostituendo ancheiltestonel readmore href?come risolvere questoproblema?it is replacing the text in the readmore href also? how to fix this?
- 1
- 2014-01-13
- Naveen
-
- 2014-12-09
Quanto soprafunzionabene. Hoeseguitoil codice simile,ma collegailtitoloe l'estrattoinsieme.Ma ho scoperto che siinterrompe quando qualcunoinserisce uno spazio "" all'inizio o allafine di untermine di una query di ricerca.
Quindi ho aggiunto questa riga:
$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');
Spero che questo si dimostri di aiutaregli altri.
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
Le soluzioniprecedentiinterrompono lapagina seiltermine di ricerca viene visualizzato all'interno deitag HTML.Dovresti usare qualcosa come:
$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);
-
grazie amicomi hai reso lagiornata :-)thanxs mate you made my day :-)
- 1
- 2016-01-26
- Agha Umair Ahmed
-
- 2020-03-02
Combina 2 risposteper utilizzarei filtri:
// 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');
Quindineltuo CSS (adesempio):
.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; }
Comepossoevidenziarei termini di ricerca senzaplug-in?