###前提・実現したいこと
search.phpにおいて、ページャーを正常に動作させたい。
###発生している問題・エラーメッセージ
当初、search.phpの2ページ目以降がどうしても表示されませんでした。
ページャーの2ページ目をクリックすると、
「**http://hogehoge/page/2/?s=検索語句**」
のURLに飛び、
どうしてもNOTFOUNDになり困り果てていました。
そこでいろいろと調べて、調整したところ、
「**http://hogehoge/?s=検索語句&page=2**」
というように飛ぶ先のURLを変更させることで、NOTFOUNDにはならず、ちゃんと検索結果が表示されるようになりました。
しかし、解決したかに見えたのですが、そこからはまることになりました。
よく見ると、2ページ目以降の全てのページがどういうわけか1ページ目と同じ内容が表示されるようになりました。
何か初歩的なミスをしているのか、どこがどう間違っているのか、かれこれ数日悩んでおり完全にお手上げ状態です。
検索に関しては県名、地域、詳細地域というようにカスタムフィールドで三点設定(region0,region1,region2)されており、地図上からクリックをして該当地域に所属する人物を検索結果として表示させるという物です。
###該当のソースコード
▼▼ご回答を頂いて自分なりに修正しました。 どこかがまだ間違っているようです。 いろいろと試しましたがもうさっぱり見当もつきません。 全文を掲載しておきます。▼▼
search.phpの全文修正後
<?php get_header(); ?> <div id="content"> <div class="siteWrapp cf"> <div class="mainCon cf"> <section> <div class="dietMembers-list"> <div class="cf"> <h3><?php echo $key ?>の検索結果一覧</h3> <?php if ( have_posts() ) : while (have_posts()) : the_post(); ?> <article> <div class="list-block cf"> <div class="left-box thumbnail-box"> <a class="thumbnail-a" href="<?php the_permalink(); ?>"><?php the_post_thumbnail(array(150,)); ?></a> </div> <div class="right-box"> <dl> <dt>所属</dt> <dd class="party-name"><?php the_category(); ?></dd> <dd><?php the_tags('','',''); ?></dd> <dd class="region"><?php echo post_custom('region1'); echo post_custom('region2'); ?></dd> </dl> <p class="name"><a href="<?php the_permalink(); ?>"><?php echo post_custom('name'); ?></a></p> <div class="link-box"> <?php $cat_slug = get_the_category(); $cat_slug = $cat_slug[0]; ?> <p><a href="<?php echo get_home_url(),"/",$cat_slug->category_nicename ?>">>地域詳細を見る</a></p> <p><a href="<?php the_permalink(); ?>">>プロフィールを見る</a></p> </div> </div> </div> </article> <?php endwhile; ?> <div class="s-page-nav cf"> <?php $big = 999999999; echo paginate_links( array( 'base' => 'http://hogehoge/?s=' . $key . '%_%', 'format' => '&page=%#%', 'current' => max( 0, get_query_var('page') ), 'total' => $the_query->max_num_pages, 'end_size' => 5, 'type' => 'list', 'show_all' => 'false' ) ); wp_reset_postdata(); ?> </div> <?php else : ?> <p class="notfound">該当者がいません。</p> <?php endif; ?> </div> </div> </section> </div> <?php get_sidebar(); ?> </div> </div> <?php get_footer(); ?>
function.phpの全文修正後
<?php function change_posts_per_page($query) { /* 管理画面,メインクエリに干渉しないために必須 */ if( is_admin() || ! $query->is_main_query() ){ return; } if ( $query->is_search() ) { $query->set( 'post_type','post' ); $query->set( 'paged',get_query_var( 'paged' ) ); $key = $query->get('s'); $taxquery = array( array( 'key' => 'region0', 'value' => $key, 'compare' => '=', 'type' => 'CHAR'), array( 'key' => 'region1', 'value' => $key, 'compare' => '=', 'type' => 'CHAR'), array( 'key' => 'region2', 'value' => $key, 'compare' => '=', 'type' => 'CHAR'), 'relation'=>'or' ); $query->set( 'tax_query' , $taxquery ); return; } } add_action( 'pre_get_posts', 'change_posts_per_page' ); // アイキャッチ画像を有効にする。 add_theme_support('post-thumbnails'); if (function_exists('add_theme_support')) { add_theme_support('post-thumbnails'); add_image_size( 'pc_single_thumbnails', 800, 500, true ); add_image_size( 'pc_list_thumbnails', 608, 380, true ); add_image_size( 'main_thumbnails', 480, 300, true ); add_image_size( 'mobile_list_thumbnails', 240, 180, true ); } //ここからポピュラーポストのカスタマイズ設定 function my_custom_single_popular_post( $post_html, $p, $instance ){ $thumbnail_id = get_post_thumbnail_id( $p->id); $thumbnail_img = wp_get_attachment_image_src( $thumbnail_id, 'main_thumbnails' ); $post_cat=get_the_category( $p->id); $cat_id=$post_cat[0]->cat_ID; $cat_name=$post_cat[0]->cat_name; $cat = get_the_category(); $cat = $cat[0]; $cat_slug = $post_cat[0]->category_nicename; $post_tag=get_the_tags( $p->id); $tag_id=$post_tag[0]->name; $custom_name = get_post_meta($p->id, 'name', true); $custom_region1 = get_post_meta($p->id, 'region1', true); $custom_region2 = get_post_meta($p->id, 'region2', true); $custom_id= $p->id; //$excerpt = get_post_field( post_content, $p->id, display ); $output = ' <article> <div class="list-block cf"> <div class="left-box thumbnail-box"> <a class="thumbnail-a" href="' . get_the_permalink($p->id) . '"><img src="' . $thumbnail_img[0] . '" title="' . esc_attr($p->title) . '" class="rank_thumbnail" width="150" alt="thumbnail"> </a> </div> <div class="right-box"> <dl> <dt>所属</dt> <dd class="party-name"><a href="' . get_category_link( $cat_id ) . '">' . $cat_name . '</a></dd> <dd>' . $tag_id . '</dd> <dd class="region">'. $custom_region1 .''. $custom_region2 .'</dd> </dl> <p class="name"><a href="' . get_the_permalink($p->id) . '">'. $custom_name .'</a></p> <div class="link-box"> <p><a href="' . get_home_url() . '/' . $cat_slug . '">>地域詳細を見る</a></p> <p><a href="' . get_the_permalink($p->id) . '">>プロフィールを見る</a></p> </div> </div> </div> </article> '; return $output; } add_filter( 'wpp_post', 'my_custom_single_popular_post', 10, 3 ); add_filter('redirect_canonical','my_disable_redirect_canonical'); function my_disable_redirect_canonical( $redirect_url ) { if ( is_search() ) $redirect_url = false; return $redirect_url; } ?>
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/11/20 07:44
2016/11/20 09:53
2016/11/20 10:09
2016/11/20 11:11
2016/11/20 12:55