现在我在搜索结果页面(search.php)中遇到了一个问题,结果是由索引页面模板显示的,而不是它的模板,它显示所有帖子,不管搜索者是否输入了内容
我的索引。php页面
<?php get_header(); ?>
<?php
$search = $_get[\'search\'];
$args = array(
    \'post_title_like\' => $search
);
$res = new wp_query($args);
if ($res -> have_posts()){
    while ($res -> have_posts()) {
        $res -> the_post();
        ?>
        <!--start section 1-->
        <section class="section1" style="margin: 0;box-shadow: 0 2px 10px">
            <div class="container">
                <a href="<?php echo get_post_permalink(); ?>">
                    <div class="img">
                        <?php the_post_thumbnail(); ?>
                    </div>
                    <h1 class="page-title"><?php the_title(); ?></h1>
                </a>
                <div class="clearfix"></div>
            </div>
        </section>
        <!--end section 1-->
        <?php the_content(); ?>
        <?php
    }
}
?>
<?php get_footer() ?>
 我的搜索。php页面
<?php get_header(); ?>
<?php
if (have_posts()){
    while (have_posts()) {
        the_post();
        ?>
        <section class="section1" rule="main">
            <div class="container">
                <div class="img">
                    <?php the_post_thumbnail(); ?>
                </div>
                <h1 class="page-title"><?php the_title(); ?></h1>
                <div class="clearfix"></div>
            </div>
        </section>
        <?php
    }
}
?>
<?php get_footer(); ?>
 如果您需要更多代码,请告诉我
 
                    最合适的回答,由SO网友:Jacob Peattie 整理而成
                    搜索。当使用?s= 查询非?search=. 确保name 搜索表单中输入的属性为s:
<input type="search" name="s">
 和您的索引。php文件绝对不应该使用这样的自定义查询。使用与搜索中相同的主循环。php。WordPress已经为您查询了正确的帖子。
if ( have_posts() ){
    while ( have_posts() ) {
        the_post();
    }
}