2016-05-10 16 views
2

カスタムポストタイプの検索結果を表示するカスタム検索フォームをセットアップしました。形式は次のとおりです。私のsearch.phpでWordpressカスタムポストタイプの検索結果ページ

<form class="search-form" action="<?php echo home_url('/'); ?>"> 
    <input type="search" name="s" placeholder="Search&hellip;"> 
    <input type="hidden" name="post_type" value="resource-library"> 
    <input type="submit" value="Search"> 
</form> 

私は、コンテンツタイプは、リソース・ライブラリである場合、検索-リソースlibrary.phpと呼ばれるカスタム検索のphpファイルに人々を指示します。私はこれをやっているが、私はおそらく検索フォームをHTMLに設定されたURL文字列からポストタイプを格納することができないよう正確に何を得る

<?php 
// store the post type from the URL string 
$post_type = $_GET['post_type']; 
// check to see if there was a post type in the 
// URL string and if a results template for that 
// post type actually exists 
if (isset($post_type) && locate_template('search-' . $post_type . '.php')) { 
    // if so, load that template 
    get_template_part('search', $post_type); 

    // and then exit out 
    exit; 
} 
?> 

:だから、私はsearch.phpページの上部にこれを置きます。

アイデア?

+0

投稿タイプを「保存」することはどういう意味ですか? '$ post_type = $ _GET ['post_type']'は投稿タイプを設定していませんか?または、他の何か? –

+0

が正しい。私は、検索フォームの送信ボタンをクリックした後に、urlにpost_typeを追加することができません。私は?post_type = research-libraryをurlに渡す必要があります。 –

+0

申し訳ありませんが、それはもっと混乱しています。 '$ post_type'は上のコードで正しい値を得ますか?そうでなければ、あなたは '$ post_type = $ _POST ['post_type'];' –

答えて

0

問題が見つかりました。私の元のコードと方法は正しいです。 「Nice Search」というプラグインがインストールされているため、urlパラメータが検索フォームから渡されていませんでした。このプラグインは?s =クエリを/ search/queryにリダイレクトし、%20を+に変換します。

関連する問題