web-dev-qa-db-ja.com

2つの検索ページ、1つの検索フォーム

私は複数のページに配置したい検索フォームを持っています(それは異なったヘッダタイプになるでしょう)私は '検索フォームの取得'機能を使ってこれを行います。私の検索フォームには、 'poster'と 'house'という2つのカスタム投稿タイプをリストしたラジオセクションがあります。

投稿タイプごとに2つの異なる検索ページがあります。私のナビゲーションには別々の検索ページを追加したいので、それらを別々にしたい。 「ポスターを探す」、「家を探す」。

エントリを送信する前に選択したラジオボタンに応じて、検索フォームからユーザーを "家を検索する"ページまたは "ポスターを検索する"ページにリダイレクトする方法はありますか。

できればSearcform.phpファイル内に必要なコーディングをすべて残しておきたいです。

1
Adzay

これが私の解決策です。ラジオボタンにOnclick属性を使用して、フォーム内の要素の「アクション」を変更しました。

<form  id="searchme" action="<?php echo site_url(); ?>/postersearch" method="get">
<ul class=" four columns inline-list offset-by-one">
  <li><label for="radio4"><input name="post_type" CHECKED type="radio" id="radio4" onclick="document.getElementById('searchme').action='<?php echo site_url(); ?>/postersearch'; document.getElementById('searchsubmit').value='Search For Posters';"/> Events</label></li>
  <li><label for="radio5"><input name="post_type"  type="radio" id="radio5" onclick="document.getElementById('searchme').action='<?php echo site_url(); ?>/housesearch'; document.getElementById('searchsubmit').value='Search For Houses';"/> Locations</label></li>
</ul>
<input id="searchsubmit" type="submit" value="Search For posters"  style="width:100%"/>             
</form>
1
Adzay

'template_include'にフックして、ここでテンプレートを変更してください。

構成されていない例、テストされていません:

add_action( 'template_include', 'wpse_96472_search_template' );

function wpse_96472_search_template( $template )
{
    if ( ! is_search() )
        return $template;

    if ( empty ( $_GET['post_type'] ) )
        return $template;

    if ( 'poster' === $_GET['post_type'] )
        return get_template_directory() . '/poster-search-template.php';

    if ( 'house' === $_GET['post_type'] )
        return get_template_directory() . '/house-search-template.php';

    return $template;
}

もちろんテンプレート名と$_GETパラメータ名を変更する必要があります。

1
fuxia