前提
wordpressで地域ポータルサイトを制作中です。
店舗個別ページはカスタム投稿で作成しました。この店舗個別ページを検索したく、以下の仕様で検索システムを実装途中です。
カスタム投稿:restaurant
カスタム分類1:area_cat(エリア指定は2階層 都道府県-市区町村)
カスタム分類2:genre_cat(ジャンル指定は2階層 親カテゴリ-子カテゴリ)
カスタム分類1に属するターム:allarea(全国を指定する際に使用、都道府県と同階層)
ページ遷移
1、トップページにて、都道府県(例:東京都)を選択
2、東京都の飲食店一覧が表示される(テンプレート:taxonomy-area_cat.phpを使用)、このページに市区町村とジャンルで絞り込むエリアを設置(東京都以下の市区町村×ジャンルでの絞り込み、例:中央区×和食など)。
3、「中央区×和食」で絞り込んだ記事一覧が表示される
上記のような流れで検索結果ページまでたどり着くことができました。
###実現したいこと
上記ページ遷移の「2」で、市区町村が選択されず、ジャンルのみ選択された場合、検索結果ページ(ページ遷移「3」)で、「東京都×和食」の結果を表示させたいです。
「市町村選択ページで、何もチェックされなかった場合、全てのタームを表示する=area_catが空だった場合、全ての子タームにチェックが入ったことにする」という考え方で、以下のコードを試しました。
該当のソースコード
taxonomy-area_cat.php
<form action="<?php echo get_post_type_archive_link('restaurant'); ?>" method="get"> <h3>エリアを選択(市町村一覧を表示しています)</h3> エリアを選択(市町村一覧を表示しています) <?php $my_id = get_queried_object_id(); ?> <?php $arg = array( 'hide_empty'=>false, 'parent'=>$my_id, 'orderby' =>'ID' ); echo my_tax_checkbox('area_cat',$arg); ?> <h3>ジャンルを選択</h3> <!-- 一括で出力する方法が分からず親タームごとに表示させました。もっとスマートな書き方があると思うのですが...--> <ul> <li><div class="main_menu"><i class="fas fa-pencil-alt"></i>和食</div> <ul class="sub_menu"> <?php $taxonomy_name = 'genre_cat'; $term_id = '1'; $termchildren = get_term_children( $term_id, $taxonomy_name ); foreach ( $termchildren as $child ) :?> <?php $term = get_term_by( 'id', $child, $taxonomy_name );?> <li><input type="radio" id="<?php echo $term->slug; ?>" name="genre_cat" value="<?php echo $term->slug; ?>"><label for="<?php echo $term->slug; ?>"><?php echo $term->name; ?></label><div class="radio-check"></div></li> <?php endforeach; ?> </ul> </li> <li><div class="main_menu"><i class="fas fa-pencil-alt"></i>イタリアン</div> <ul class="sub_menu"> <?php $taxonomy_name = 'genre_cat'; $term_id = '2'; $termchildren = get_term_children( $term_id, $taxonomy_name ); foreach ( $termchildren as $child ) :?> <?php $term = get_term_by( 'id', $child, $taxonomy_name );?> <li><input type="radio" id="<?php echo $term->slug; ?>" name="genre_cat" value="<?php echo $term->slug; ?>"><label for="<?php echo $term->slug; ?>"><?php echo $term->name; ?></label><div class="radio-check"></div></li> <?php endforeach; ?> </ul> </li> </ul> <input type="submit" value="検索"> </form>
functions.php
//複合検索 function my_tax_checkbox($tax,$arg) { $qv = get_query_var($tax); $terms = get_terms($tax,$arg); $output = ''; foreach($terms as $t) { $checked = ''; if(is_array($qv)) { if(in_array($t->slug,$qv)) $checked = ' checked'; } else { if($t==$qv) $checked = ' checked'; } $output .= '<li class="list__item"><label class="label--checkbox"><input type="checkbox" class="checkbox" name="'.$tax.'[]" value="'.$t->slug.'"'.$checked.'><span>'.$t->name.'</span></label>'."\n"; } echo $output; }
search.php
(他の方の過去の質問を参考にし、見よう見まねで入力してみました)
<?php $tax01 = $_GET['area_cat']; $tax02 = $_GET['genre_cat']; /* 条件取得 */ if( !empty($tax01) ) { $taxquerysp = array( 'taxonomy'=>'area_cat', 'terms'=>$tax01, 'field'=>'slug', 'operator'=>'AND' );}; if($tax01){ $taxquerysp = array( 'taxonomy'=>'area_cat', 'terms'=> $tax01, 'field'=>'slug', 'operator'=>'AND' ); } if($tax02){ $taxquerysp = array( 'taxonomy'=>'genre_cat', 'terms'=> $tax02, 'field'=>'slug', 'operator'=>'AND' ); } query_posts( array( 'paged' => $paged , 'posts_per_page' => 10, 'post_type' =>'restaurant', 's' => $s, 'tax_query' => array( $taxquerysp, 'relation' => 'AND' ) ) ); ?> <?php if ( have_posts() ) : ?> <?php while ( have_posts() ) : the_post(); ?> 〜
そもそもformを記載するテンプレートがいけない!なんてこともあるのでしょうか。
ウェブで検索するも色々な情報があり、混乱しています。。
かれこれ1週間悩み続けております。。
どなたか助けてください!!!
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。