Evidenziazione Termine

⚠️ Nota: Questa estensione è OBSOLETA a partire dalla versione 3.0 di SearchWP e verrà rimossa a breve ⚠️

Si prega di consultare questa documentazione: https://searchwp.com/docs/settings/highlight/

Se attivo, Evidenziazione Termine eliminerà automaticamente il Titolo e l'Estratto restituiti dalle ricerche e racchiuderà i termini di ricerca con uno span, permettendoti di evidenziarli in modo che corrispondano bene al design del tuo sito.

Evidenziazione Termine include anche una funzione che ti permette di recuperare un estratto dal post_content o da un Campo Personalizzato che contenga almeno uno dei termini di ricerca. Vedi sotto come usarlo.

Utilizzo

L'installazione di questa Estensione è la stessa di tutte le altre Estensioni. Scarica il file zip dal tuo Account, carica e installa come faresti con qualsiasi altro plugin di WordPress. Evidenziazione Termine apparirà nella tua lista Plugin nell'area di amministrazione di WordPress e potrai attivarla.

Una volta attivato, Evidenziazione Termine evidenzierà automaticamente i termini all'interno del Titolo e dell'Estratto per le ricerche native di WordPress. Le evidenziazioni vengono applicate racchiudendo i termini in <span class="searchwp-highlight" />, che puoi targettizzare nel tuo CSS.

Evidenziazione Termine sovrascriverà anche automaticamente le chiamate a the_excerpt() sostituendo quel contenuto con un estratto che contenga almeno un termine di ricerca dal contenuto principale (ad es. il campo contenuto principale, non i Campi Personalizzati). Evidenzierà anche i termini in questo contenuto. Se desideri disattivare o personalizzare questa funzionalità, consulta i filtri sottostanti.

Puoi fare un passo in più utilizzando Evidenziazione Termine per generare un estratto da una serie di potenziali fonti (inclusi i Campi Personalizzati) filtrando automaticamente le chiamate a the_excerpt() nelle pagine dei risultati di ricerca in questo modo:

<?php
function searchwp_term_highlight_auto_excerpt( $excerpt ) {
global $post;
if ( ! is_search() ) {
return $excerpt;
}
// prevent recursion
remove_filter( 'get_the_excerpt', 'searchwp_term_highlight_auto_excerpt' );
$global_excerpt = searchwp_term_highlight_get_the_excerpt_global( $post->ID, null, get_search_query() );
add_filter( 'get_the_excerpt', 'searchwp_term_highlight_auto_excerpt' );
return wp_kses_post( $global_excerpt );
}
add_filter( 'get_the_excerpt', 'searchwp_term_highlight_auto_excerpt' );
view raw functions.php hosted with ❤ by GitHub

Se invece vuoi sostituire manualmente le chiamate puoi usare qualcosa di simile a questo:

<?php
/* SearchWP Term Highlight offers an alternative to the_excerpt()
If you're looking to generate any sort of excerpt based on one of the following:
- the actual excerpt
- the generated excerpt from the post content
- the post content itself
- string-based custom field values
this function is for you. It will try to find one of the above (in that order)
and output something with at least one search term in it (if possible)
*/
// echo the excerpt (designed to be used IN PLACE OF the_excerpt
if( function_exists( 'searchwp_term_highlight_the_excerpt_global' ) ) {
searchwp_term_highlight_the_excerpt_global();
}
// return the excerpt (designed to be used IN PLACE OF get_the_excerpt
if( function_exists( 'searchwp_term_highlight_get_the_excerpt_global' ) ) {
$excerpt = searchwp_term_highlight_get_the_excerpt_global();
}
view raw gistfile1.php hosted with ❤ by GitHub

Motori di Ricerca Supplementari

Evidenziazione Termine non evidenzia automaticamente i termini per le ricerche dei Motori di Ricerca Supplementari! Dovrai personalizzare il tuo template inviando il tuo contenuto attraverso l'evidenziatore prima di visualizzarlo, in questo modo:

<?php
// this snippet is based on SearchWP's sample Supplemental Search Engine results template found here:
// https://searchwp.com/docs/configuration/#supplemental
// **************************** NOTE ****************************
// this snippet is just a portion of your search results template
// **************************** NOTE ****************************
// these are the search terms
$query = isset( $_REQUEST['swpquery'] ) ? sanitize_text_field( $_REQUEST['swpquery'] ) : '';
$highlighter = false;
if( class_exists( 'SearchWP_Term_Highlight' ) ) {
$highlighter = new SearchWP_Term_Highlight();
}
?>
<?php foreach ( $posts as $post ): setup_postdata( $post ); ?>
<div class="post">
<h2>
<a href="<?php echo get_permalink(); ?>">
<?php
// highlight the title
$title = get_the_title();
if( $highlighter ) {
$title = $highlighter->apply_highlight( $title, $query );
}
echo wp_kses_post( $title );
?>
</a>
</h2>
<?php
// output an excerpt
$excerpt = get_the_excerpt();
if( $highlighter ) {
$excerpt = $highlighter->apply_highlight( $excerpt, $query );
}
echo wp_kses_post( $excerpt );
?>
<div class="custom-field-content">
<?php
// output highlighted content from a Custom Field
$custom_field = get_post_meta( get_the_ID(), 'my_custom_field_key', true );
if( $highlighter ) {
$custom_field = $highlighter->apply_highlight( $custom_field, $query );
}
echo wp_kses_post( $custom_field );
?>
</div>
</div>
<?php endforeach; ?>
view raw gistfile1.php hosted with ❤ by GitHub

NOTA: lo snippet sopra è inteso a rappresentare la personalizzazione che dovresti fare al template di esempio dei risultati del Motore di Ricerca Supplementare sostituendo SOLO l'output dei risultati di ricerca, non è un template di pagina intero.

Filtri

Ci sono un paio di filtri da notare quando si utilizza Evidenziazione Termine:

searchwp_th_auto_filter_excerpt
Imposta se Evidenziazione Termine sostituisce automaticamente the_excerpt() con un estratto contenente i termini di ricerca. Il valore predefinito è true, per disabilitare:

<?php
add_filter( 'searchwp_th_auto_filter_excerpt', '__return_false' );
view raw gistfile1.php hosted with ❤ by GitHub

searchwp_th_num_words
Definisci il numero di parole che Evidenziazione Termine includerà quando costruisce un estratto che contiene almeno un termine di ricerca. Il valore predefinito è 55.

<?php
function my_searchwp_th_num_words() {
// use 75 words instead of the default 55
return 75;
}
add_filter( 'searchwp_th_num_words', 'my_searchwp_th_num_words' );
view raw gistfile1.php hosted with ❤ by GitHub

searchwp_th_excluded_engines
Indica a Evidenziazione Termine di saltare l'evidenziazione dei termini per determinati motori di ricerca. Il valore predefinito è nessuno.

<?php
function my_searchwp_th_excluded_engines( $load_posts, $search_args ) {
// DO NOT highlight terms when using the 'my_supplemental' search engine
return array( 'my_supplemental' );
}
add_filter( 'searchwp_th_excluded_engines', 'my_searchwp_th_excluded_engines', 10, 2 );
view raw gistfile1.php hosted with ❤ by GitHub

searchwp_th_password_required_message
Se un post è protetto da password, Term Highlight restituisce una stringa personalizzata. Il valore predefinito è quello predefinito di WordPress: Non c'è un estratto perché questo è un post protetto..

<?php
function my_searchwp_th_password_required_message() {
return __( 'This entry is private.' );
}
add_filter( 'searchwp_th_password_required_message', 'my_searchwp_th_password_required_message' );
view raw gistfile1.php hosted with ❤ by GitHub

Crea Oggi Stesso una Migliore Esperienza di Ricerca WordPress

Non perdere mai più visitatori a causa di risultati di ricerca inutili. SearchWP rende la creazione della tua ricerca intelligente WordPress veloce e facile.

Get SearchWP Now
Icona di motori di ricerca multipli