キーワード検索結果を自分の作った任意のページで表示したく、search.php,searchform.phpのコードをコピーして同様のコードになったはずなのですが、検索結果が一件以上出てくるキーワードを入れたはずにもかかわらず、
「こちらには何もありません。検索をお試しください。」と表示されてしまいます。何故なのでしょうか。
//フォームを入力するページ(form action先は、my-search.php) <?php $unique_id = esc_attr( uniqid( 'search-form-' ) ); ?> <form role="search" method="get" class="search-form" action="<?php echo esc_url( home_url( '/my-search' ) ); ?>"> <label for="<?php echo $unique_id; ?>"> <span class="screen-reader-text"><?php echo _x( 'Search for:', 'label', 'twentyseventeen' ); ?></span> </label> <input type="search" id="<?php echo $unique_id; ?>" class="search-field" placeholder="<?php echo esc_attr_x( 'Search …', 'placeholder', 'twentyseventeen' ); ?>" value="<?php echo get_search_query(); ?>" name="s" /> <button type="submit" class="search-submit"><?php echo twentyseventeen_get_svg( array( 'icon' => 'search' ) ); ?><span class="screen-reader-text"><?php echo _x( 'Search', 'submit button', 'twentyseventeen' ); ?></span></button> </form>
//my-search.php(search.phpと同じコード) <?php /** * The template for displaying search results pages * * @link https://developer.wordpress.org/themes/basics/template-hierarchy/#search-result * * @package WordPress * @subpackage Twenty_Seventeen * @since 1.0 * @version 1.0 */ get_header(); ?> <div class="wrap"> <header class="page-header"> <?php if ( have_posts() ) : ?> <h1 class="page-title"><?php printf( __( 'Search Results for: %s', 'twentyseventeen' ), '<span>' . get_search_query() . '</span>' ); ?></h1> <?php else : ?> <h1 class="page-title"><?php _e( 'Nothing Found', 'twentyseventeen' ); ?></h1> <?php endif; ?> </header><!-- .page-header --> <div id="primary" class="content-area"> <main id="main" class="site-main" role="main"> <?php if ( have_posts() ) : /* Start the Loop */ while ( have_posts() ) : the_post(); /** * Run the loop for the search to output the results. * If you want to overload this in a child theme then include a file * called content-search.php and that will be used instead. */ get_template_part( 'template-parts/post/content', 'excerpt' ); endwhile; // End of the loop. the_posts_pagination( array( 'prev_text' => twentyseventeen_get_svg( array( 'icon' => 'arrow-left' ) ) . '<span class="screen-reader-text">' . __( 'Previous page', 'twentyseventeen' ) . '</span>', 'next_text' => '<span class="screen-reader-text">' . __( 'Next page', 'twentyseventeen' ) . '</span>' . twentyseventeen_get_svg( array( 'icon' => 'arrow-right' ) ), 'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'twentyseventeen' ) . ' </span>', ) ); else : ?> <p><?php _e( 'Sorry, but nothing matched your search terms. Please try again with some different keywords.', 'twentyseventeen' ); ?></p> <?php get_search_form(); endif; ?> </main><!-- #main --> </div><!-- #primary --> </div><!-- .wrap --> <?php get_footer();
my-search.phpはテンプレートで固定ページにしました。(テンプレートにするのは忘れていました。)スラッグは分かりやすくするために、my-searchにしました。これでも同じですが記事が表示されないです。
テンプレートページではなく、独自のファイル名でやりたい様ですので、可能であればその辺りの理由など頂ければと思います。search.php 以外に実装するという意味でも、他テンプレートで表示するのか独自テンプレートを実装するのかでは大分難易度が違います。
単に検索結果を表示するだけでなく、自分が作った、年月やタクソノミーで記事の条件を指定する機能を追加したいからです。つまり、キーワードで検索してそのキーワードで検索された記事を条件として、さらにその記事の中からセレクトボックスで月の指定をしたりしたいからです。
検索結果が一つのページだけで表示ならばわざわざ新しいファイルは必要ないですが、複数のページに検索結果を表示する必要があるので、どうしても規定テンプレートだけでは解決できないということです。先ほどの追記です。
回答3件
あなたの回答
tips
プレビュー