カテゴリー、タクソノミーでの絞り込み検索結果一覧のページ送りを指定表示件数以下の時非表示にしたい
WordPressで自作テーマを作成しています。
Custom Post Type UI で作成した「college」というカスタム投稿の内容を、カテゴリーで検索、タクソノミーで検索、キーワードで検索と三つの方法で検索できるよう作業を進めています。
検索結果は表示できるようになったのですが、検索結果に該当する投稿が指定している件数以下の時もページ送りがずっと表示されている状態で困っています。
タクソノミーとカテゴリーで絞込み検索をした時に常時表示され、キーワード検索をした時は該当する記事が指定している件数以下の時消えます。キーワード検索をした時のように指定件数以下の時には表示されないようにしたいです。
関係していそうな箇所のphpソースを記載いたします。
functions.php
/* 検索結果ページにページネーション追加 */ function page_navi() { global $wp_query; $bignum = 999999999; if ( $wp_query->max_num_pages <= 1 ) return; echo '<div class="pagenation">'; echo paginate_links( array( 'base' => str_replace( $bignum, '%#%', esc_url( get_pagenum_link($bignum) ) ), 'format' => '?paged=%#%', 'current' => max( 1, get_query_var('paged') ), 'total' => $wp_query->max_num_pages, 'prev_text' => '<i class="fa fa-caret-left"></i>', 'next_text' => '<i class="fa fa-caret-right"></i>', 'type' => 'list', 'end_size' => 3, 'mid_size' => 3 ) ); echo '</div>'; }
search.php
<?php $s = $_GET['s']; $catnum = ""; $post_tag = ""; if(filter_input(INPUT_GET, 'catnum')) { $catnum = $_GET['catnum']; } if(filter_input(INPUT_GET, 'subject')) { $post_tag = $_GET['subject']; } if($post_tag){ $taxquerysp[] = array( 'taxonomy'=>'colleges', 'terms'=> $post_tag, 'include_children'=>false, 'field'=>'slug', 'operator'=>'AND', ); } if($catnum){ $taxquerysp[] = array( 'taxonomy'=>'category', 'terms'=> $catnum, 'include_children'=>false, 'field'=>'term_id', 'operator'=>'AND', ); } else { $taxquerysp[] = array( 'taxonomy'=>'category', 'terms'=> 'colleges', 'include_children'=>false, 'operator'=>'OR', ); } ?> <?php $args = array( 'post_type'=>'college', 'tax_query' => $taxquerysp, 's' => $s, 'post_status' => 'publish', 'order' => 'ASC', 'orderby' => 'meta_value', 'meta_key' => 'yomi', 'paged'=>get_query_var('paged'), 'posts_per_page' => 5, ); $q = new WP_Query( $args ); ?> <section class="list"> <h3>「<?php the_search_query(); ?><?php if($catnum){ ?><?php echo get_cat_name($catnum); } ?><?php if($post_tag){ ?><?php echo get_term_by('slug',$post_tag,"post_tag")->name; } ?>」の検索結果</h3> <?php if ( $q->have_posts() ) : while ( $q->have_posts() ) : $q->the_post(); ?> <div class="list__box clearfix"> ループ内容 </div> <!-- /.list__box --> <?php endwhile; else : ?> <p>申し訳ございません。該当する記事がございません。</p> <?php endif; ?> </section> <?php page_navi(); ?> <?php wp_reset_query(); ?>
searchform.php
<!-- カテゴリー検索 --> <form method="get" action="<?php echo esc_url( home_url( '/' ) ); ?>"> <input type="hidden" name="s" id="s01" /> <select name="catnum"> <option value="">選択してください</option> <?php $categories = get_categories(); foreach($categories as $category) : ?> <option value="<?php echo $category->term_id; ?>"><?php echo $category->cat_name; ?></option> <?php endforeach; ?> </select> <input id="submit" type="submit" value="" accesskey="f" /> </form> <!-- タクソノミー検索 --> <form role="search" method="get" id="searchform" action="<?php echo home_url( '/' ); ?>"> <input type="hidden" name="s"> <input type="hidden" name="post_type" value="college" /> <select name="subject"> <option value="">選択してください</option> <?php $taxonomy_name = 'colleges'; $taxonomys = get_terms($taxonomy_name); if(!is_wp_error($taxonomys) && count($taxonomys)): foreach($taxonomys as $taxonomy): $tax_posts = get_posts(array('post_type' => get_post_type('college'), 'taxonomy' => $taxonomy_name, 'term' => $taxonomy->slug ) ); if($tax_posts): ?> <option value="<?php echo $taxonomy->slug; ?>"><?php echo $taxonomy->name; ?></option> <?php endif; endforeach; endif; ?> </select> <input type="submit" id="searchsubmit" value="" /> </form> <!-- キーワード検索 --> <form method="get" id="searchform" action="<?php echo home_url('/'); ?>"> <input type="text" name="s" id="testSearchInput" value="<?php the_search_query(); ?>" placeholder="入力例:保育士" /> <input type="hidden" name="post_type" value="college"> <input type="submit" value="" accesskey="f" /> </form>
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/05/29 08:31
2018/05/29 08:33