$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>search results in custom template search.php|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

search results in custom template search.php

matteradmin6PV0评论

right now i have a problem in the search result page (search.php) the result is shown by the index page template not its template and it display all the posts despite it have what the searcher enter or not

my index.php page

<?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() ?>

my search.php page

<?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(); ?>

if you need more code please tell me

right now i have a problem in the search result page (search.php) the result is shown by the index page template not its template and it display all the posts despite it have what the searcher enter or not

my index.php page

<?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() ?>

my search.php page

<?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(); ?>

if you need more code please tell me

Share Improve this question asked Nov 23, 2018 at 11:36 abdo sayedabdo sayed 31 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

The search.php template is used, and the correct posts returned, when a search is performed with the ?s= query. Not ?search=. Make sure the name attribute of the input in your search form is s:

<input type="search" name="s">

And your index.php file should absolutely not be using a custom query like that. Use the main loop the same way you have in search.php. WordPress already queries the correct posts for you.

if ( have_posts() ){
    while ( have_posts() ) {
        the_post();
    }
}

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far