Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
WPBカップ
25 Million+
Websites using our plugins
16+
Years of WordPress experience
3000+
WordPress tutorials
by experts

WordPressで検索トグル効果を追加する方法

編集メモ: WPBeginner のパートナーリンクから手数料を得ています。手数料は編集者の意見や評価に影響を与えません。編集プロセスについて詳しく知る。

WordPressに検索トグル効果を追加したいですか?

検索トグル効果により、フォームの代わりに検索アイコンを表示することができます。アイコンをクリックすると、微妙なスライドアニメーションとともに検索フォームが表示されます。

この投稿では、WordPressで検索トグル効果を簡単に追加する方法を紹介します。

Adding search toggle effect in WordPress

WordPressの検索トグル効果とは?

検索トグル効果は、WordPressサイトでの検索体験を向上させるウェブデザインのテクニックです。

検索フォームを表示する代わりに、検索アイコンを表示します。ユーザーがそれをクリックすると、検索フォームがスライドアウトアニメーションとともに表示されます。

Search toggle example

WordPressの初期設定の検索フォームは見た目があまりよくありません。多くのトップWordPressテーマはすでに初期設定のフォームを独自のスタイルに置き換えています。

しかし、もしあなたのテーマがまだ静的でプレーンな検索フォームを使っているなら、このチュートリアルを使ってよりユーザーフレンドリーにすることができます。

ということで、WordPressで検索トグル効果を簡単に追加する方法を見てみよう。

注:このチュートリアルは、HTML / CSSの基本的な理解を必要とするため、中・上級レベルのユーザー向けです。

WordPressに検索トグル効果を追加する

WordPressで検索トグル効果を追加するには、サイトに検索フォームブロックまたはウィジェットを設置し、カスタマイザーコードを追加する必要があります。

まず、あなたのサイトに検索フォームが追加されていることを確認する必要があります。

フルサイト編集可能なブロックテーマに検索フォームを追加する

フルサイト編集機能付きの新しいWordPressブロックテーマを使っている場合は、この方法でサイトに検索フォームを追加します。

外観 ” エディターページにアクセスするだけです。編集画面で、検索フォームを表示したい場所に「検索」ブロックを追加します。

Add search form block

その後、「保存」ボタンをクリックして変更を保存することを忘れないでください。

他のWordPressテーマで検索フォームを追加する

フルサイト編集機能がないWordPressテーマの場合、検索ブロックやウィジェットをサイドバーやウィジェット対応エリアに追加することができます。

外観 ” ウィジェットページに行き、検索フォームを表示したいサイドバーに「検索」ブロック/ウィジェットを追加する。

Add search widget to a sidebar

次に、検索アイコン用の透過画像を探す必要がある。

このチュートリアルでは、この画像を使用します。暗い背景に向いています(ご自分のサイトでご自由にお使いください)。

独自の検索アイコンを作成する必要がある場合は、寸法が50×50ピクセル以下の透過PNG画像を作成すると良いでしょう。

画像アイコンを入手したら、それをサイトにアップロードする必要があります。メディア ” 新規追加ページに行き、検索アイコン画像をアップロードしてください。

Upload search icon

アップロードしたら、「URLをクリップボードにコピー」ボタンをクリックし、このURLをメモ帳やテキストエディターなどのプレーンテキストエディターにペーストします。

これですべてが整ったので、WordPressサイトにカスタムCSSを追加する必要があります。

このチュートリアルでは、カスタムCSSを保存するためにWPCode プラグインを使用することをお勧めします。

WPCodeを使用すると、WordPressでカスタムコードスニペットを簡単に追加することができます。使い方は簡単で、テーマの更新や別のテーマに切り替えた場合でも、コードが影響を受けることはありません。

まず、無料のWPCodeプラグインをインストールし、有効化する必要があります。詳しくは、WordPressプラグインのインストール方法のステップバイステップガイドをご覧ください。

有効化したら、WordPress管理ダッシュボードのCode Snippetsページにアクセスする必要があります。そして、一番上の「新規追加」ボタンをクリックしてください。

Add new snippet

次の画面では、まずこのコード・スニペットのタイトルを入力します。このコード・スニペットを識別するのに役立つものであれば何でもかまいません。

その後、「コードタイプ」を「HTMLスニペット」に変更する。カスタムCSSをHTMLを使ってサイトのヘッダーに読み込みます。

Adding snippet

次に、以下のコードを「コード・プレビュー」ボックスに貼り付ける。

<style type="text/css">
.wp-block-search__inside-wrapper  
.wp-block-search__input {
background-color: transparent;
background-image: url(/url/to/search-icon.png);
background-position: 5px center;
background-repeat: no-repeat;
background-size: 24px 24px;
border: none;
cursor: pointer;
height: 37px;
margin: 3px 0;
padding: 0 0 0 34px;
position: relative;
-webkit-transition: width 400ms ease, background 400ms ease;
transition: width 400ms ease, background 400ms ease;
width: 0;
}
 
.wp-block-search__inside-wrapper  
.wp-block-search__input:focus {
background-color: #fff;
border: 2px solid #c3c0ab;
cursor: text;
outline: 0;
width: 230px;
}
.search-form
.search-submit { 
display:none;
}
</style>

重要です: /url/to/search-icon.pngを先ほどコピーした画像URLに置き換えることを忘れないでください。

その後、Inactiveトグルを’Active’に切り替え、’Save Snippet’ボタンをクリックします。

Activate and save snippet

あなたのサイトにアクセスして、トグル効果のある検索フォームを見ることができます。

Search toggle effect in action on a WordPress block theme

検索フォームのトグル効果に関するトラブルシューティング

上記の方法は、ウィジェットブロックを使用するほとんどの最新のWordPressテーマで動作します。

ただし、テーマによってはこの方法が使えない場合もある。

WordPressテーマの検索フォームにwp-block-search__inside-wrapperクラスとwp-block-search__inputクラスがない場合に発生する可能性があります。

その場合、WordPressテーマで使用されているCSSクラスを見つけるために、inspectツールを使用する必要があります。

例えば、下のスクリーンショットでは、デモテーマが検索フォームにこれらのCSSクラスを使用しています。

Finding CSS classes

以下のコードを修正し、代わりにこれらのCSSクラスを使用することができます。以下はその例です。

<style type="text/css"> 
.search-wrap input.s  {
    background-color: transparent;
    background-image: url(/url/to/search-icon.png);
    background-position: 5px center;
    background-repeat: no-repeat;
    background-size: 24px 24px;
    border: none;
    cursor: pointer;
    height: 37px;
    margin: 3px 0;
    padding: 0 0 0 34px;
    position: relative;
    -webkit-transition: width 400ms ease, background 400ms ease;
    transition:         width 400ms ease, background 400ms ease;
    width: 0;
}
 
.search-wrap input.s:focus {
    background-color: #fff;
    border: 2px solid #c3c0ab;
    cursor: text;
    outline: 0;
    width: 230px;
}
.search-form
.search-submit { 
display:none;
}
</style>

テストサイトではこんな感じだった。

Search toggle with classic search widget

ボーナスのヒントWordPressでサイト内検索を強化しよう

トグル効果はWordPressの検索フォームをきれいに見せるだけです。サイトの検索結果の質には影響しません。

初期設定のWordPressには、非常に基本的な検索機能がついています。この検索機能は遅く、不正確で、しばしば空白の結果を表示します。

SearchWPの出番だ。

市場で最高のWordPress検索プラグインであり、初期設定の検索機能を強力な検索エンジンに簡単に置き換えることができます。

SearchWP Homepage

SearchWPは自動的に検索フォームを置き換えるので、テーマファイルを編集する必要はありません。サイト上のあらゆる場所で一致を探し、検索結果の質を劇的に向上させます。

また、ユーザーがクエリーを入力すると即座に結果を表示するライブ検索エクステンションが付属しています。さらに、eコマースをフルサポートしているので、あなたのオンラインストアでスマートなWooCommerce検索体験を作ることができます。

この投稿がWordPressで検索トグル効果を追加する方法を学ぶのにお役に立てば幸いです。WordPressのコンバージョントラッキングガイドや、サイトで最も人気のある投稿を見つける方法もご覧ください。

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

情報開示 私たちのコンテンツは読者支援型です。これは、あなたが私たちのリンクの一部をクリックした場合、私たちはコミッションを得ることができることを意味します。 WPBeginnerの資金源 をご覧ください。3$編集プロセスをご覧ください。

Avatar

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi with over 16 years of experience in WordPress, Web Hosting, eCommerce, SEO, and Marketing. Started in 2009, WPBeginner is now the largest free WordPress resource site in the industry and is often referred to as the Wikipedia for WordPress.

究極のWordPressツールキット

ツールキットへの無料アクセス - すべてのプロフェッショナルが持つべきWordPress関連製品とリソースのコレクション!

Reader Interactions

37件のコメント返信を残す

  1. Syed Balkhi says

    Hey WPBeginner readers,
    Did you know you can win exciting prizes by commenting on WPBeginner?
    Every month, our top blog commenters will win HUGE rewards, including premium WordPress plugin licenses and cash prizes.
    You can get more details about the contest from here.
    Start sharing your thoughts below to stand a chance to win!

  2. Osh says

    Hi,

    I’m using a child theme, should i add those codes to the parent theme’s files?

    I tried to do that and it is not working, please help !!!

    Thanks

  3. Benjamin Rutledge says

    Is it possible to implement this only on mobile? My theme has a nice animated search function on desktop, but only a toggle to show/hide search box in place of my logo on mobile.

  4. Beth Terry says

    Hi. Thank you very much for this code. I did have to play around with the positioning to get the search button to appear where I want it, and since I only wanted it to appear in the mobile version, I also added css to the media query for over 800px screens to show the full search field instead of the icon. I got it working great, but I do think it’s a little complicated for folks who just want something to basically plug and play.

    You can view the results here:

    To everyone who doubts, javascript is definitely not needed.

    • Beth Terry says

      Also, I want to add that I am using a Twenty Twelve child theme, which comes with the HTML4 search code. Adding the HTML5 support to the functions.php did not fix it, so I copied and pasted the HTML5 search code that you posted above into my theme header where I wanted to search box to be.

  5. Emily G says

    Hi there – implementing this on a website and would like to use FontAwesome instead of an image. I cannot get the icon to show yup, however. Do you have any tips? I am adding the font and icon font-family: FontAwesome;
    content: “\f002″; to the .search-form input[type=”search”]

    Thanks!

  6. Steve says

    I was so hoping this would work for me. I pasted in the code in the Additional CSS area of my theme (twentytwelvechild), added the search-icon.png to my theme folder using FTM and added add_theme_support(‘html5’, array(‘search-form’)); to my theme’s functions.php file. I see no change and wonder what I have missed. I hope someone can offer me a clue. thank you.

  7. Crowd says

    Thank you very much, works like a charm.
    Do you think there’s a way to make the search field appear from right to left (unlike from left to right like now)?

  8. Cassy says

    I got this working on genesis but my search form is in my nav bar, and when the screen width is relatively too small, the search form moves down.

    • Cassy says

      I got it working on Genesis. You have to move the search-icon.png into your genesis child theme images folder. Then, depending on where you put your search form, mine is in the nav bar so it is slightly different, you adjust the css style to your likes. I had to modify the “.genesis-nav-menu .search input” class to get it working.

      • Laura says

        Hi Cassy, Can you give a little more info on how you modified the .genesis-nav-menu .search input class? I can’t seem to get it working. Thanks!

    • Anur says

      yea the same problem im on genesis with enteprise pro theme doesnt work.
      Anyone who has solution to make this work on genesis..

  9. Roy says

    this is really great! Is it possible to get the icon to sit static at the right while the form slides out to the left? I can’t seem to figure it out!

  10. Joshua Farr says

    Greetings – Thank you for this great bit of code. I’ve tried inserting it into a client’s site that I’m working, using the HTML5 version of the code, I placed the extra bit into functions.php and added all of the css. I put in a search icon and got the form to show up how I want on the site – it will even do all of the toggle-ing effect as desired – but when I enter text that I want to search to test it out, I can not submit the search to view search results. We are using a woocommerce theme, “Mystile”, which has a default search form (which works when entered) but I much prefer the looks and functionality of the form as mentioned on this page.

    We tried putting the wocommerce search form code side by side with the code you gave above, tried swapping out bits and pieces to “merge” the two…but no luck.

    Any reccomendations would be extremely helpful.
    Thank you!
    -Josh

  11. Rajeesh Nair says

    Now that’s some interesting stuff there. Though I am a mere beginner and I actually use the search option that has come default with the blog theme which is like a pop-up (not exactly a pop-up though as its CSS and not javascript) and displays search bar.

    But what I am exactly looking for is that how to display Google Search along with Blog search as found in popular blogs like ListVerse and others. Please guide me through this!

  12. alexisnicholson says

    Code looks fabulous , I appreciate your efforts , surely gonna try this one for my word press project .

    Codingbrains

  13. Shahraar Khan says

    A demo would be helpful.. Also what does the use of role=”search” attribute in the form tag, what is the attribute “role” used for?

  14. Susan Silver says

    I have been looking for a simple tutorial on this. Thanks! Have you done one yet on the sliding menus like those on the Facebook mobile app? These menus are becoming very popular, even for desktop apps.

返信を残す

コメントありがとうございます。すべてのコメントは私たちのコメントポリシーに従ってモデレートされ、あなたのメールアドレスが公開されることはありませんのでご留意ください。名前欄にキーワードを使用しないでください。個人的で有意義な会話をしましょう。