検索フォームを設置し、検索結果を表示させる

1.まずは検索フォームを設置します

searchform.php

<form method="get" id="searchform" action="<?php bloginfo('url'); ?>" >
<input type="text" value="<?php the_search_query(); ?>" name="s" id="s" size="30" />
<?php wp_dropdown_categories('show_option_all='.__('ALL').'&hide_empty=1');?>
<input type="submit" id="submit" value="検索">
</form>

2.次に検索結果を取得、表示させる

search.phpの検索結果を表示させる部分

<h2>検索結果:<?php the_search_query(); ?></h2>
    <?php     
    global $query_string;
    query_posts($query_string . "&post_type=post&showposts=20");
    ?>   



    <?php if (have_posts()) : ?>
        <?php while (have_posts()) : the_post(); ?>    <!-- キーワードに合った記事を表示させる処理 -->

<div class="search_result">
           <h3> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<div class="post_meta">
<?php the_time('Y年n月j日'); ?> | カテゴリ: <?php the_category(' '); ?>
</div>
                    <p><?php echo mb_substr(get_the_excerpt(), 0, 100); ?></p>
 <div class="more_button"><a href="<?php the_permalink(); ?>">この記事を読む</a></div>
</div>
    <?php endwhile; ?>
 
    <?php else: ?>    <!--  キーワードが見つからないときの処理 -->
            <p>キーワードはみつかりません。</p>


    <?php endif; ?>
投稿日:
カテゴリー: WordPress

コメントする

メールアドレスが公開されることはありません。

CAPTCHA