前提・実現したいこと
レンタルサーバーでWordPress5.2.2を使用しています。
テーマは「WorldStar」を使用しています。
https://ja.wordpress.org/themes/worldstar/
複数のタグとキーワードを組み合わせたサイト内検索をしたいと考えています。
発生している問題・エラーメッセージ
エラーメッセージは出ていません。
検索自体は正常に動いています。
しかし、検索結果のページナビゲーションが検索結果件数にかかわらず、全記事分のページがある状態になってしまいます。
そして、ページの先へ行っても、検索結果の1ページ目と同じページが出てしまいます。
サンプルコードが検索結果の全件を表示するもののようなので、ページ送りについては考慮されていないと予想しています。
しかし、解決法が分からず、困っています。
試したこと
1.下記サイトのコードを使用して、serchform.phpとsearch.phpを書き換えました。
https://cotodama.co/taxonomy_checkbox/
searchform.php
PHP
1<form role="search" method="get" class="search-form" action="<?php echo home_url( '/' ); ?>"> 2 <label> 3 <span class="screen-reader-text"><?php echo _x( 'Search for:', 'label' ) ?></span> 4 <input type="search" class="search-field" placeholder="<?php echo esc_attr_x( 'Search …', 'placeholder' ) ?>" value="<?php echo get_search_query() ?>" name="s" title="<?php echo esc_attr_x( 'Search for:', 'label' ) ?>" /> 5 </label> 6 7<!-- フォームの追加はじめ --> 8<?php 9$my_taxonomy = 'post_tag'; //タクソノミーをスラッグで指定 10$terms = get_terms($my_taxonomy); 11if($terms):foreach($terms as $term): 12?> 13<label><input type="checkbox" name="term_slug[]" value="<?php echo $term->slug; ?>"><?php echo $term->name; ?></label><br /> 14<?php endforeach; endif; ?> 15<input type="hidden" name="my_taxonomy" value="<?php echo $my_taxonomy ?>"> 16<select name="and_or" id="and_or"> 17 <option value="AND">AND</option> 18 <option value="IN">OR</option> 19</select> 20<!-- フォームの追加おわり --> 21 22 <input type="submit" class="search-submit" value="<?php echo esc_attr_x( 'Search', 'submit button' ) ?>" /> 23</form> 24
search.php
PHP
1<?php 2/** 3 * The template for displaying search results pages. 4 * 5 * Learn more: http://codex.wordpress.org/Template_Hierarchy 6 * 7 * @package WorldStar 8 */ 9get_header(); 10// Get Theme Options from Database. 11$theme_options = worldstar_theme_options(); 12?> 13 <section id="primary" class="content-area"> 14 <main id="main" class="site-main" role="main"> 15 <?php 16 if ( have_posts() ) : ?> 17 <header class="page-header"> 18 <h2 class="archive-title"><?php printf( esc_html__( 'Search Results for: %s', 'worldstar' ), '<span>' . get_search_query() . '</span>' ); ?></h> 19 </header><!-- .page-header --> 20 <div id="post-wrapper" class="post-wrapper clearfix"> 21 22<?php 23$s = $_GET['s']; 24$my_taxonomy = $_GET['my_taxonomy']; 25$term_slug = $_GET['term_slug']; 26$and_or = $_GET['and_or']; 27 28if($term_slug){ 29 $tax_query[] = array( 30 'taxonomy'=> $my_taxonomy, 31 'terms'=> $term_slug, 32 'include_children'=> false, 33 'field'=> 'slug', 34 'operator'=> $and_or 35 ); 36} 37$args = array( 38 'tax_query' => $tax_query, 39 's' => $s, 40 'posts_per_page' => -1 41); 42$the_query = new WP_Query( $args ); ?> 43<?php if ( $the_query->have_posts() ) : ?> 44 45<!-- ループはじめ --> 46 <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?> 47 <?php 48 if ( 'post' === get_post_type() ) : 49 get_template_part( 'template-parts/content', $theme_options['post_content'] ); 50 else : 51 get_template_part( 'template-parts/content', 'search' ); 52 endif; 53 ?> 54 <?php endwhile; ?> 55<!-- ループおわり --> 56 </div> 57<?php worldstar_pagination(); ?> 58<?php wp_reset_postdata(); ?> 59<?php else : ?> 60get_template_part( 'template-parts/content', 'none' ); 61<?php endif; ?> 62 <?php 63 else : 64 get_template_part( 'template-parts/content', 'none' ); 65 endif; ?> 66 </main><!-- #main --> 67 </section><!-- #primary --> 68 <?php get_sidebar(); ?> 69<?php get_footer(); ?>
2.検索結果件数の変更
参考ページに「'posts_per_page'は、1ページあたりの表示件数を指定します。ここでは全件表示を指定しています。」と書かれていたので、-1を10に変更してみましたが、最初の10件が表示されるだけで、次ページへ移っても最初の10件が表示されてしまいます。
3.search.php の重複と思われる下記の行を削除するとエラーになってしまいます。
PHP
1 <?php 2 else : 3 get_template_part( 'template-parts/content', 'none' ); 4 endif; ?>
補足情報(FW/ツールのバージョンなど)
サーバーの仕様
Apache 2.2
PHP 5.6.40
MySQL 5.5.62
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/07/18 04:38
2019/07/26 10:52