SearchWP のモーダル検索フォーム拡張機能!で、このようなモーダルフォームをサイトに追加しましょう。
⚠️ 注意: この拡張機能は SearchWP バージョン 3.0 以降非推奨となり、まもなく削除されます ⚠️
こちらのドキュメントをご覧ください: https://searchwp.com/docs/settings/highlight/
有効にすると、タームハイライトは検索から返されたタイトルと抜粋を自動的にクリーニングし、検索語をspanで囲みます。これにより、サイトのデザインによく合うように強調表示できます。
タームハイライトには、検索語を少なくとも1つ含むpost_contentまたはカスタムフィールドから抜粋を取得できる機能も含まれています。使い方は以下のとおりです。
この拡張機能のインストールは、他のすべての拡張機能と同じです。アカウントからzipファイルをダウンロードし、他のWordPressプラグインと同様にアップロードしてインストールします。タームハイライトはWordPress管理エリアのプラグインリストに表示され、有効化できます。
有効にすると、タームハイライトはネイティブWordPress検索内で自動的にタイトルと抜粋の用語を強調表示します。ハイライトは<span class="searchwp-highlight" />で用語を囲むことによって適用され、CSSでターゲットにすることができます。
タームハイライトは、メインコンテンツ(例:メインコンテンツフィールド、カスタムフィールドではありません)の少なくとも1つの検索語を含む抜粋でそのコンテンツを置き換えることにより、the_excerpt()への呼び出しを自動的にオーバーライドします。このコンテンツの用語も強調表示します。この機能を無効化またはカスタマイズしたい場合は、以下のフィルターを参照してください。
以下のように、検索結果ページでthe_excerpt()への呼び出しを自動的にフィルター処理することにより、多数の潜在的なソース(カスタムフィールドを含む)から抜粋を生成するためにタームハイライトを利用することで、さらに一歩進めることができます。
| <?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' ); |
代わりに手動で呼び出しを置き換えたい場合は、次のようなものを使用できます。
| <?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(); | |
| } |
タームハイライトは、補足検索エンジンの検索に対して用語を自動的に強調表示しません!テンプレートをカスタマイズして、以下のように出力する前にコンテンツをハイライターに通す必要があります。
| <?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; ?> |
注意:上記の抜粋は、サンプル補足検索エンジンの結果テンプレートに必要なカスタマイズを表すことを目的としており、検索結果の出力のみを置き換えるものであり、完全なページテンプレートではありません。
タームハイライトを使用する際に注意すべきフィルターがいくつかあります。
searchwp_th_auto_filter_excerpt
タームハイライトが検索語を含む抜粋でthe_excerpt()を自動的に置き換えるかどうかを設定します。デフォルトはtrueです。無効にするには:
| <?php | |
| add_filter( 'searchwp_th_auto_filter_excerpt', '__return_false' ); |
searchwp_th_num_words
検索語を少なくとも1つ含む抜粋を作成する際に、タームハイライトが含める単語数を定義します。デフォルトは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' ); |
searchwp_th_excluded_engines
特定の検索エンジンで用語のハイライトをスキップするようにタームハイライトに指示します。デフォルトはnoneです。
| <?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 ); |
searchwp_th_password_required_message
投稿がパスワードで保護されている場合、Term Highlight はカスタム文字列を返します。デフォルトは WordPress コアのデフォルトです: この投稿は保護されているため、抜粋はありません。。
| <?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' ); |
役に立たない検索結果で訪問者を失うことはもうありません。SearchWPを使用すると、独自のスマートなWordPress検索をすばやく簡単に作成できます。
Get SearchWP Now