現在wordpressにてカスタムタクソノミーとチェックボックスを使ったWORKSの絞り込み検索を制作しているのですが、
archive.phpで絞り込みをし、検索結果をsearch.phpで表示するようにしているのですが、その際絞り込みは
できているのですが、archive側でチェックしたチェックボックスをserach側でもチェックされたまま遷移させたいのですが
調べてみてもなかなか出てこなく、実装方法などご存知のかたなどおりましたらご教授お願いします。
また,index.phpに表示されているWORKSの記事に紐付いてるタクソノミーをクリックした際にも
遷移先でそのクリックしたタクソノミーのチェックボックスにチェックされている状態にしたいのですが
どのようにすれば良いでしょうか。
index.phpのWORKS部分 <section class="section section-project"> <h2 class="section-title section-title__project">Project</h2> <div class="project-wrapper"> <ul class="project-list"> <?php global $query_string; query_posts( Array( 'post_type' => array( 'works' ), 'tax_query' => array( array( 'taxonomy' => 'pickup', //タクソノミー名を指定 'field' => 'slug', 'terms' => 'pickup', 'operator' => 'IN' //'AND'、'IN'、'NOT IN'の何れかを指定 ) ), // 'showposts' => 5, 'posts_per_page' => 6, 'paged' => get_query_var('paged') ) ); if (have_posts()) { while (have_posts()) { the_post(); ?> <li class="project-listItem f-right"> <a href="<?php the_permalink(); ?>"> <div class="project-img"> <?php the_post_thumbnail(); ?> </div> <h3> <span><?php the_title(); ?></span> </h3> </a> <div class="project-meta"> <ul class="meta-list"> <?php $terms = get_the_terms($post->ID,'service'); foreach( $terms as $term ) { echo '<li><a href="'.get_term_link($term->slug,'service').'">'.$term->name.'</a></li>'; } ?> <?php $terms = get_the_terms($post->ID,'categorytag'); foreach( $terms as $term ) { echo '<li><a href="'.get_term_link($term->slug,'categorytag').'">'.$term->name.'</a></li>'; } ?> <?php $terms = get_the_terms($post->ID,'people'); foreach( $terms as $term ) { echo '<li><a href="'.get_term_link($term->slug,'people').'">'.$term->name.'</a></li>'; } ?> </ul> </div> <a href="<?php the_permalink(); ?>" class="btn btn-project btn-more"> <span>more</span> </a> </li> <?php } }else{ } wp_reset_query(); ?> </ul> <a href="<?php echo home_url("/works"); ?>" class="btn btn-all"> <span>All Projects</span> </a> </div> </section>
archive-works.phpの該当部分 <div class="project-wrapper"> <ul class="project-list"> <?php $my_query = new WP_Query(array('post_type' => 'works','posts_per_page' => -1));?> <?php /*------ 条件定義 カテゴリ/表示件数 ---- */?> <?php if($my_query->have_posts()): ?> <?php /*------ LOOP START ---- */?> <?php while($my_query->have_posts()): $my_query->the_post();?> <li class="project-listItem"> <a href="<?php the_permalink(); ?>"> <div class="project-img"> <div style="background-image:url('<?php $image = get_field('kv'); if( !empty($image) ){ echo $image['sizes'][ "medium" ]; } ?>')"></div> </div> <p class="project-meta"> <?php the_time('Y.m.d'); ?> </p> <h3 class="project-title"> <span><?php the_title(); ?></span> </h3> </a> <ul class="meta-list meta-list__project"> <?php $terms = get_the_terms($post->ID,'service'); foreach( $terms as $term ) { echo '<li><a href="'.get_term_link($term->slug,'service').'">'.$term->name.'</a></li>'; } ?> <?php $terms = get_the_terms($post->ID,'categorytag'); foreach( $terms as $term ) { echo '<li><a href="'.get_term_link($term->slug,'categorytag').'">'.$term->name.'</a></li>'; } ?> <?php $terms = get_the_terms($post->ID,'people'); foreach( $terms as $term ) { echo '<li><a href="'.get_term_link($term->slug,'people').'">'.$term->name.'</a></li>'; } ?> </ul> <a href="<?php the_permalink(); ?>" class="btn btn-project btn-more"> <span>more</span> </a> </li> <?php endwhile; ?> <?php endif; ?> </ul> </div>
search.php <?php get_search_form(); ?> <div class="project-wrapper"> <ul class="project-list"> <?php //絞り込みの値を取得 $s = $_GET['s']; $post_type = $_GET['works']; $service = $_GET['service']; $category = $_GET['category']; $people = $_GET['p']; //絞り込みの値をクエリ用に代入 if( !empty($service) ) { $service_selected = array('taxonomy'=>'service','terms'=>$service,'field'=>'slug','operator'=>'IN'); } if( !empty($category) ) { //タグの場合 $category_selected = array('taxonomy'=>'categorytag','terms'=>$category,'field'=>'slug','operator'=>'IN'); } if( !empty($people) ) { //タグの場合 $people_selected = array('taxonomy'=>'people','terms'=>$people,'field'=>'slug','operator'=>'IN'); } //タクソノミー絞り込みの場合はクエリを指定 if( !empty($service) || !empty($category) ) { query_posts( array( 'paged' => $paged, 'post_type' => $post_type, 'posts_per_page' => -1, 's' => get_search_query(), 'tax_query' => array( 'relation' => 'AND', $service_selected, $category_selected, $people_selected ) ) ); } ?> <?php if( have_posts() ): while ( have_posts() ) : the_post(); ?> <li class="project-listItem"> <a href="<?php the_permalink(); ?>"> <div class="project-img"> <div style="background-image:url('<?php $image = get_field('kv'); if( !empty($image) ){ echo $image['sizes'][ "medium" ]; } ?>')"></div> </div> <p class="project-meta"> <?php the_time('Y.m.d'); ?> </p> <h3 class="project-title"> <span><?php the_title(); ?></span> </h3> </a> <ul class="meta-list meta-list__project"> <?php $terms = get_the_terms($post->ID,'service'); foreach( $terms as $term ) { echo '<li><a href="'.get_term_link($term->slug,'service').'">'.$term->name.'</a></li>'; } ?> <?php $terms = get_the_terms($post->ID,'categorytag'); foreach( $terms as $term ) { echo '<li><a href="'.get_term_link($term->slug,'categorytag').'">'.$term->name.'</a></li>'; } ?> <?php $terms = get_the_terms($post->ID,'people'); foreach( $terms as $term ) { echo '<li><a href="'.get_term_link($term->slug,'people').'">'.$term->name.'</a></li>'; } ?> </ul> <a href="<?php the_permalink(); ?>" class="btn btn-project btn-more"> <span>more</span> </a> </li> <?php endwhile; else: endif; ?> </ul> </div>
searchform.php <div class="project-search"> <div class="s-close"> <span></span> <span></span> </div> <h3>Search</h3> <form method="get" action="<?php echo home_url('/'); ?>works/" class="search-form"> <label class="all"><input type="checkbox" name="all" value="all"><span>All Tags</span></label> <div class="search-wrapper"> <h4>Service</h4> <ul class="tag-wrapper"> <?php $terms = get_terms('service'); foreach ( $terms as $term ) : ?> <li class="tag-listItem"> <label for="<?php echo $term->slug; ?>"> <input type="checkbox" name="service[]" value="<?php echo $term->slug; ?>" id="<?php echo $term->slug; ?>"> <span><?php echo esc_html( $term->name ); ?></span> </label> </li> <?php endforeach; ?> </ul> </div> <div class="search-wrapper"> <h4>Category</h4> <ul class="tag-wrapper"> <?php $terms = get_terms('categorytag'); foreach ( $terms as $term ) : ?> <li class="tag-listItem"> <label for="<?php echo $term->slug; ?>"> <input type="checkbox" name="category[]" value="<?php echo $term->slug; ?>" id="<?php echo $term->slug; ?>"> <span><?php echo esc_html( $term->name ); ?></span> </label> </li> <?php endforeach; ?> </ul> </div> <div class="search-wrapper"> <h4>People</h4> <ul class="tag-wrapper"> <?php $terms = get_terms('people'); foreach ( $terms as $term ) : ?> <li class="tag-listItem"> <label for="<?php echo $term->slug; ?>"> <input type="checkbox" name="p[]" value="<?php echo $term->slug; ?>" id="<?php echo $term->slug; ?>"> <span><?php echo esc_html( $term->name ); ?></span> </label> </li> <?php endforeach; ?> </ul> </div> <input type="hidden" name="s" value="<?php the_search_query(); ?>"> <div class="search-btn"> <input type="submit" value="submit" class="submit-button"> <input type="reset" value="clear" class="submit-button reset-button"> </div> </form> </div>
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。