Very short trick: WordPress, highlight the results of a search

There are several plugins for WordPress that allow to improve the features and performance of the function "Search". Almost all the plugins available are useful and attractive feature highlight search terms in the search results. If you're not going to appensantire further your WordPress installation, but do not want to give up everything to a similar feature, here's how to easily implement a "highlights" of termii search results.

. Open the file search.php , and insert the following code inside the loop center or, if you use a standard theme, look for the div class="entry" . In practice we would modify the output of the result after a search:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<div class="entry">
<? Php
/ / The variable $ s is the search string
/ / For example: parolaA parolaB
explode ( " " , $s ) ; $ Keys = explode ("", $ s);
/ / Fetch the summary, not the whole text
get_the_excerpt ( ) ; $ Excerpt = get_the_excerpt ();
/ / Wrap in the summary the search words
/ / With <strong class="evidenzia"> parolaA </ strong>
preg_replace ( '/(' . implode ( '|' , $keys ) . ')/iu' , '<strong class="evidenzia">\0</strong>' , $excerpt ) ; $ Excerpt = preg_replace ('/ ('. implode ('|', $ keys). ') / iu', '<strong class="evidenzia"> \ 0 </ strong>', $ excerpt);
/ / Monster altered the summary
"the_excerpt" , $excerpt ) ; echo apply_filters ("the_excerpt", $ excerpt);
?>
?> ">Leggi tutto</a></p> <p class="right more"> <a href = "<? php the_permalink ()?>"> Read more </ a> </ p>
</ Div>

giallino simile ad un evidenziatore… o quello che volete) nel risultato se presenti nel riassunto . All the words entered in the search field will be highlighted (the CSS class evidenzia you can define it as desired, for example with a background similar to a yellow highlighter ... or whatever) in result if present in the summary. This procedure, in fact, it is limited to act only on the first part of our post (excerpt), not all of its contents. However, it is a good alternative to installing an additional plugin.

11 comments: "Very short trick: WordPress, highlight the results of a search"

  1. July 9, 2009 Undolog.com "Very short trick: Wordpress, the results highlight ... :

    [...] Learn more visit the source: Undolog.com "Very short trick: WordPress, the results highlight ... Related articles: Undolog.com" Very short trick: WordPress, exclude pages and post [...]

  2. July 12, 2009 The best of the week # 25 | BigThink :

    [...] Very short trick: WordPress, highlight the results of a search a little trick to raise the profile of the top keyword searches on our blog in search results. [...]

  3. July 15, 2009 YesWEBcan :

    Really interesting, thanks.
    Looking at your blog I see that is really well done, you insert between the sites to visit my
    Hello and congratulations

  4. July 19, 2009 # 4 Best of the Web - Luca Merchants :

    [...] Blogging @ @ Very short trick: WordPress, highlight the results of a search [...]

  5. October 6, 2009 jicke:

    @ YesWEBcan:

    Really interesting, thanks.
    Looking at your blog I see that is really well done, you insert between the sites to visit my
    Hello and congratulations

    thank cool to post

  6. September 6, 2010 Sp0nky:

    Bohh .. in search.php data entry .. I'm not indicate where you can insert this code?

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    ; ?> <? Php get_header ();?>

    <div id="content">
    <div class="post">
    <div class="title">
    <h2> Rezultatele cautarii </ h2>
    </ Div>
    ( have_posts ( ) ) : ?> <? Php if (have_posts ()):?>

    ( have_posts ( ) ) : the_post ( ) ; ?> <? Php while (have_posts ()): the_post ();?>
    ; ?> "> <Div class = "post content" id = "post-<? Php the_ID ();?>">

    ? rel = "nofollow" > " rel=" bookmark " title=" Permanent Link to <?php the_title ( ) ; ?> "> <?php the_title ( ) ; ?> </a></h3> <h3> <a href = "<? php the_permalink ()? rel =" nofollow ">" rel = "bookmark" title = "Permanent Link to <? php the_title ();?> "> <? php the_title () ;?> </ a> </ h3>
    ; ?> <? Php the_excerpt ();?>
    </ Div>
    ; ?> <? Php endwhile;?>

    <div class="navigation">
    'Previous Entries' ) ?> </span> <span class="next-entries"> <?php previous_posts_link ( 'Next Entries' ) ?> </span> <span class="previous-entries"> <? php next_posts_link ('Previous Entries')?> </ span> <span class="next-entries"> <? php previous_posts_link ('Next Entries')?> </ span>
    </ Div>

    : ?> <? Php else:?>
    It seems <h3> rau nu am gasit nimic.Va rugam incercati din nou. </ h3>
    ; ?> <? Php endif;?>
    </ Div> <! --/content ->

    </ Div> <! --/content ->

    ; ?> <? Php get_sidebar ();?>

    ; ?> <? Php get_footer ();?>
  7. September 6, 2010 Giovambattista Fazioli :

    @ Sp0nky: I did not understand the question ...

  8. September 6, 2010 Sp0nky:

    Giovambattista my dear, in your article writing and you can open the search.php inserirsci the following code inside .....
    , per non sbagliare ho chiesto dove inserisco il codice postato di te? As you can see in my search.php , I have div class="entry" , to avoid mistakes I asked where I post the code you posted?

  9. September 6, 2010 Giovambattista Fazioli :

    @ Sp0nky: Ok, you have to put into practice in line 14 instead of:

    1
    ; ?> <? Php the_excerpt ();?>

    This is because - as written in the post - you're not using the standard theme. Each theme, in fact, may occasionally alter the HTML standard WordPress, although many issues remain the nomenclature entry .

  10. September 6, 2010 Sp0nky:

    thanks a lot amico.Hai many interesting things in here I have to try and put sito.Li practica.A soon.

  11. February 10, 2011 Don Kazim:

    Hello and thanks for this nice article! ^ ^
    I have a problem:
    I want in the search results there is both the_excerpt (), and both the_content (),.
    in this way:
    the_content ();
    the_excerpt ();

    Thank you ^ ^

    PS: Is there any code to improve the search?

Leave a comment

TAG XHTML PERMITS: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> INSERTION CODE:
 <pre></pre> // blocco generico <code></code> // blocco generico [cc_actionscript][/cc_actionscript] // Actionscript [cc_actionscript3][/cc_actionscript3] // Actionscript 3 [cc_css][/cc_css] // CSS Style Sheet [cc_html][/cc_html] // HTML [cc_js][/cc_js] // Javascript [cc_objc][/cc_objc] // Objective-C [cc_php][/cc_objc] // PHP [cc_sql][/cc_sql] // SQL 


Stop SOPA