12:44 AM投稿記事の長さ:X JAPANの紅 × 8.6個 くらい

WordPressのget_search_form()が効かなかったお話

今回はこのブログを立ち上げる時に苦労した部分でも書こうかなぁ、と思って一番最初に思いついたのがこれ。

ネット上を飛び回っても中々解決しなかったのだが、同じ悩みを抱えている人の一助にでもなればと思い……いや、こんなミスをするのは自分だけかもしれない。

症状:トップページ以外の検索フォームで調べた検索結果が0件だった時、ループ内が真っ白になる

多分、自らsearchform.phpとかをカスタマイズしてる人にありがちなミスなのではないのだろうか。自分もその一人だった。

理想

検索結果がある場合は問題なし。



無事問題を木っ端微塵にし、開通したブログの姿である。

そして肝心の、検索結果が見つからない時。



「サイト閉鎖」なんていうアリエナイ不吉な事を検索されても、めげずに自身のtwitterアカウントをオススメするという。
こういう時に卑屈になってはいけない、自分の土俵ではどーんと構えろ、とは母ちゃんの教えである。

現実




現実は厳しい。
開通前から閉鎖騒ぎである。騒ぎと書いたが、sectionタグの中はじつに静まり返っている。

コードと、ネットから拾ってきた解決策

まず、この症状によくあるらしい、間違ったsearchform.phpから。

[php]
<form role="search" method="get" id="searchform" class="searchform" action="">
<div>
<label class="screen-reader-text" for="s"><?php _x( ‘Search for:’, ‘label’ ); ?></label>
<input type="text" value="<?php echo get_search_query(); ?>" name="s" id="s" />
<input type="submit" id="searchsubmit" value="<?php echo esc_attr_x( ‘Search’, ‘submit button’ ); ?>" />
</div>
</form>
[/php]

問題は一行目、formタグの中にあるaction属性が未指定な点。

もしこの症状に苦しんでいる方がこの記事を読んでいらっしゃるなら、検索してページが切り替わった時点でアドレスバーを見て欲しい。


この画像のハイライトされた部分のように、間に余計なモノが入っていると0件だった場合の検索結果は正常に表示されない。

正しくは、http://noji.dev/?s=サイト閉鎖と書かれていれば正常な表示になるはずだ。
そのためにはコードを次のように修正する。

[php]
<form role="search" method="get" id="searchform" class="searchform" action="<?php bloginfo(‘home’); ?>">
<div>
<label class="screen-reader-text" for="s"><?php _x( ‘Search for:’, ‘label’ ); ?></label>
<input type="text" value="<?php echo get_search_query(); ?>" name="s" id="s" />
<input type="submit" id="searchsubmit" value="<?php echo esc_attr_x( ‘Search’, ‘submit button’ ); ?>" />
</div>
</form>
[/php]

action=”<?php bloginfo(‘home’); ?>”といった具合に、テンプレートタグを挟んでやる。
これで、検索をする際に切り替わるページが自動的にトップページとなり、正常な検索結果が得られる訳だ。

でも、直らない

ここからが本番である。
ネット上で入手できた対策方法は上記のようなモノがほとんどだった(他にはプラグインの一時停止とか)。

が、いくら試してもループの中身は真っ白けだし、開発ツールで見たformタグ内のaction属性は=””という始末。

searchform.phpを呼び出しているget_search_form()があるsidebar.php(自分の場合は。人によってはheader.phpやfunction.php)にもちゃんとget_search_form()は存在している。

はて、おかしいな……と思ってコードとにらめっこすること30分。気がついた。

間違えた部分

[php]
//sidebar.phpの一部から抜粋
//略
</div>
<form id="ser" class="search">
<?php get_search_form(); ?>
</form>
<div id="gra" class="graphics">
<h3>Graphics</h3>
//略
[/php]

お分かりいただけただろうか……

これを展開すると、

[html]
//略
</div>
<form id="ser" class="search">
<form method="get" id="searchform" action="http://noji.dev/" >
<div>
<input type="text" value="" name="s" id="s" />
<input type="submit" id="searchsubmit" value="検索" />
</div>
</form>
</form>
<div id="gra" class="graphics">
<h3>Graphics</h3>
//略
[/html]

formの入れ子になってしまうのである。
実際には子要素のformはなかったことにされ、親のformタグ(actionが未指定の)が中のdivタグをくくってしまっていたのだった。

というわけで、正しいコードがこちら。

[php]
//sidebar.php(現在使用中)の一部から抜粋
//略
</div>
<?php get_search_form(); ?>
<div id="gra" class="graphics">
<h3>Graphics</h3>
//略
[/php]

まとめ

灯台下暗しとは誰しもがする経験だが、ことコーディングという作業は特にそれが多い。

今回のような、別の人がみれば簡単に判明しそうな問題も、本人にとっては見つけ辛いものである。
フォーラムや知恵袋等の「遠くの知人(この場合は知識ある赤の他人、とこじつけさせてほしい)」を頼りにするのもありだが、その前に「近くの知人(この場合は普通の知人)」へ問題の部分を見てもらうと、灯台の影になっているミスがあらわになる事もあるのではないだろうか。

いや、今回のミスは酷すぎるのだけれども。

Comments

  1. 81bet1 より:

    Gave 81bet1 a quick peek. Design’s okay. See if the odds are worth your time before you commit, ya know? 81bet1

  2. 212betapp より:

    Gave 212betapp a whirl. Honestly? Not bad. The mobile experience is smooth, and the promotions are actually decent. Worth a look if you’re in the market for a new betting app. See for yourself: 212betapp

  3. qq2 game より:

    Yo, QQ2 Game looks pretty sick! Gonna give it a whirl tonight, see what’s up. Fingers crossed for some wins! Check it out at qq2 game

  4. 666betlogin より:

    Heading over to 666betlogin. Wishing myself luck! Need to win back what I lost last week lol. Give it a shot here: 666betlogin

  5. betmadrid より:

    Checking out Betmadrid to place some bets on the upcoming games. Hoping for a good payout! See for yourself here: betmadrid

  6. af888 より:

    Alright, AF888, let’s do this! Gonna sign up and see what the fuss is about. Hoping for some beginner’s luck. af888

  7. id888app より:

    Id888app? Yeah, not gonna lie, it’s pretty solid. Quick payouts, decent selection of games. I like that it’s easy to use on my phone. Give it a try if you’re lookin’ for something new id888app.

  8. 99jl より:

    Alright, 99jl ain’t too bad. Signed up last week and already cashed out a few times. The site’s pretty straightforward, nothing too fancy. Could use a few more promos, but overall, thumbs up 99jl.

  9. phwinslotlogin より:

    Phwinslotlogin has a streamlined login process which is pretty slick. Gets you right into the games. Fast in and fast out! Test the waters at: phwinslotlogin

  10. phwinapp より:

    Yo, download the phwinapp, it’s fire! Easy to use and you can play anywhere. Seriously, get on it! phwinapp

  11. cardtimegames より:

    Cardtimegames got all the games you need! So many choices, you won’t get bored. Trust me, it’s worth it! cardtimegames

  12. phph143 より:

    Super fast loading times and very user friendly. I love the local touch they have on the platform. Cheers to phph143 for the great service.

  13. ca777game より:

    Such an exciting place to play! I love the thrill of the jackpots and the community vibe is great. My go-to site is ca777game

  14. sierracasino より:

    [6101]sierracasino অফিসিয়াল লগইন | সেরা স্লট গেম ও দ্রুত বিকাশ পেমেন্ট,sierracasino-তে আজই যোগ দিন। আমাদের অ্যাপ ডাউনলোড করে লাইভ ক্যাসিনো গেমের নিয়ম জানুন এবং দ্রুত বিকাশ পেমেন্টের সুবিধা উপভোগ করুন। এখনই শুরু করুন! visit: sierracasino

  15. pk2jbet より:

    This site completely changed my expectations of what online entertainment should feel like. Games load in a flash and the payment gateway handles every currency I need without any fuss. Fair odds and honest terms make this my daily stop. You have to try pk2jbet and feel the difference yourself.

  16. whalecasino より:

    [3477]whalecasino অফিসিয়াল লগইন | সেরা স্লট গেম ও বিকাশ ডিপোজিট,whalecasino অ্যাপ ডাউনলোড করে সেরা অনলাইন স্লট গেম খেলুন। বিকাশ দিয়ে সহজ ডিপোজিট এবং রুলেট খেলার নিয়ম শিখে আজই জয়েন করুন। visit: whalecasino

  17. phuujili より:

    I’ve tried a few platforms lately and this one really stands out. The interface is smooth and the payouts are surprisingly fast. Definitely worth checking out phuujili

  18. n1casino より:

    [6938]n1casino Official Login | সেরা স্লট গেম ও বিকাশ ডিপোজিট,n1casino-তে আজই যোগ দিন। আমাদের অ্যাপ ডাউনলোড করে সেরা অনলাইন স্লট গেম খেলুন এবং বিকাশ দিয়ে দ্রুত ডিপোজিট করে অভিজ্ঞতা নিন। এখনই লগইন করুন। visit: n1casino

  19. I was struggling to get back into my account but this alternative link saved the day. Super easy to navigate. Check out zuma78linkalternatif if you are having access issues.

  20. wib4d より:

    [9760]wib4d | Login Resmi Link Alternatif Slot Gacor Terpercaya,wib4d menyediakan link alternatif resmi untuk akses slot gacor hari ini. Nikmati kemudahan daftar casino terpercaya dengan opsi deposit via dana. Gabung sekarang! visit: wib4d

  21. je7777login より:

    I’ve been trying a few sites lately but this one is definitely the smoothest. The login process is fast and I didn’t hit any glitches. Highly recommend je7777login for anyone looking for a stable experience.


Warning: Undefined variable $aria_req in /var/www/html/noji.dev/wp-content/themes/ODD_CODES/comments.php on line 15

Warning: Undefined variable $aria_req in /var/www/html/noji.dev/wp-content/themes/ODD_CODES/comments.php on line 17