ヘッダーのカスタマイズ
検索フォーム
検索フォームを設置したい場合は関数get_serch_form()を記述
<?php get_search_form(); ?>
テーマ内にsearchform.phpがあれば、それを読み込み、なければ、WP標準仕様のサイト内検索フォームが表示されます。
自作の検索フォームを読み込む
searchform.phpを作成(基本)
<form role="search" method="get" id="searchform" class="searchform" action="<?php echo home_url(); ?>"> <div> <label class="screen-reader-text" for="s">検索:</label> <input type="text" value="<?php the_search_query(); ?>" name="s" id="s"> <input type="submit" id="searchsubmit" value="検索"> </div> </form>
カテゴリによる絞り込み機能をつける
WP関数wp_dropdown_categories()を使うとセレクトボックスによるカテゴリリストが表示される
<form role="search" method="get" id="searchform" class="searchform" action="<?php echo home_url(); ?>"> <div> <label class="screen-reader-text" for="s">検索:</label> <?php $argument = array( 'show_option_none' => 'カテゴリーを選択', 'orderby' => 'name', 'hide_empty' => 0 ); wp_dropdown_categories($argument); ?> <input type="text" value="<?php the_search_query(); ?>" name="s" id="s"> <input type="submit" id="searchsubmit" value="検索"> </div> </form>
タグによる絞り込み機能をつける
<?php $tags = get_tags(); if($tags){ ?> <select name="tag" id="tag"> <option value="" selected="selected">タグを選択</option> <?php foreach($tags as $tag){ ?> <option value="<?php echo esc_html($tag->slug); ?>"><?php echo esc_html($tag->name); ?></option> <?php } ?> </select> <?php } ?>