WORDPRESS | Surligner les résultats de recherche WordPress avec jQuery

Ce hack PHP pour WordPress vous permettra de mettre en surbrillance les mots-clés dans les résultats de recherche de votre site WordPress.
utilise avec letheme The Professional de elegantthemes

Nous utiliserons jQuery, un langage intégré en natif dans WordPress.

  • etape 1 : Ajout d’une commande PHP dans functions.php
/* --- HIGHLIGHT SEARCH with JQUERY --- */
function hls_set_query() {
$query = attribute_escape(get_search_query());

if(strlen($query) > 0){
echo '
<script type="text/javascript">
var hls_query = "'.$query.'";
</script>
';
}
}

function hls_init_jquery() {
wp_enqueue_script('jquery');
}

add_action('init', 'hls_init_jquery');
add_action('wp_print_scripts', 'hls_set_query');
/* --- END HIGHLIGHT SEARCH with JQUERY --- */

Étape n°2 – Édition du fichier header.php

chercher la fin de balise

</head>

et ajouter juste avant

<!-- HIGHLIGHT SEARCH with JQUERY -->
<style type="text/css" media="screen">
.hls { background: #fced6c; }
</style>
<script type="text/javascript">
jQuery.fn.extend({
highlight: function(search, insensitive, hls_class){
var regex = new RegExp("(<[^>]*>)|(\\b"+ search.replace(/([-.*+?^${}()|[\]\/\\])/g,"\\$1") +")", insensitive ? "ig" : "g");
return this.html(this.html().replace(regex, function(a, b, c){
return (a.charAt(0) == "<") ? a : "<strong class=\""+ hls_class +"\">" + c + "</strong>";
}));
}
});
jQuery(document).ready(function($){
if(typeof(hls_query) != 'undefined'){
$("#content-area").highlight(hls_query, 1, "hls");
}
});
</script>
<!-- END HIGHLIGHT SEARCH with JQUERY -->

source : http://wpchannel.com/surligner-resultats-recherche-wordpress-jquery/

Olivier
About

Product and graphic designer turned to be a Mac SysAdmin.

Posted in Design, Development Tagged with: , , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>