最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to display search results in search.php

programmeradmin0浏览0评论

I am trying to display search results in search.php , but no results showing. it just show this search results for: then the keyword I put in the search box only nothing more. so the actual results for the keyword not showing from the posts.

I have two files. here are

search.php

<?php get_header();  ?>

     <h5><b>SEARCH RESULTS</b></h5>
     <h3 class="mt-30 mb-15">SEARCH RESULTS FOR YOUR KEYWORD</h3>



<h1><?php printf( __( 'Search Results for: %s'), '<span>' . get_search_query() . '</span>' ); ?></h1>


<?php get_footer();  ?>

and searchform.php

<form action="/" method="get">
    <label for="search">Search in <?php echo home_url( '/' ); ?></label>
    <input type="text" name="s" id="search" value="<?php the_search_query(); ?>" />
</form>


so what should I do?

I am trying to display search results in search.php , but no results showing. it just show this search results for: then the keyword I put in the search box only nothing more. so the actual results for the keyword not showing from the posts.

I have two files. here are

search.php

<?php get_header();  ?>

     <h5><b>SEARCH RESULTS</b></h5>
     <h3 class="mt-30 mb-15">SEARCH RESULTS FOR YOUR KEYWORD</h3>



<h1><?php printf( __( 'Search Results for: %s'), '<span>' . get_search_query() . '</span>' ); ?></h1>


<?php get_footer();  ?>

and searchform.php

<form action="/" method="get">
    <label for="search">Search in <?php echo home_url( '/' ); ?></label>
    <input type="text" name="s" id="search" value="<?php the_search_query(); ?>" />
</form>


so what should I do?

Share Improve this question edited Jun 2, 2020 at 9:18 fuxia 107k38 gold badges255 silver badges459 bronze badges asked Jun 1, 2020 at 17:34 socialsocial 713 silver badges9 bronze badges
Add a comment  | 

1 Answer 1

Reset to default -1

get_search_query() returns only the string that the user searched for. To display the search results, you'll need to do some more work.

The search results will be in $wp_query, and you can manipulate it using The Loop (like any other page).

The following code is adapted from WordPress's own Twenty Twenty theme's index.php file, and can be adapted for your theme's search.php file.

if ( is_search() ) {
        global $wp_query;

        $archive_title = sprintf(
                '%1$s %2$s',
                '<span class="color-accent">' . __( 'Search:', 'twentytwenty' ) . '</span>',
                '&ldquo;' . get_search_query() . '&rdquo;'
        );

        if ( $wp_query->found_posts ) {
                $archive_subtitle = sprintf(
                        /* translators: %s: Number of search results. */
                        _n(
                                'We found %s result for your search.',
                                'We found %s results for your search.',
                                $wp_query->found_posts,
                                'twentytwenty'
                        ),
                        number_format_i18n( $wp_query->found_posts )
                );
        } else {
                $archive_subtitle = __( 'We could not find any results for your search. You can give it another try through the search form below.', 'twentytwenty' );
        }
}

if ( have_posts() ) {

        $i = 0;

        while ( have_posts() ) {
                $i++;
                if ( $i > 1 ) {
                        echo '<hr class="post-separator styled-separator is-style-wide section-inner" aria-hidden="true" />';
                }
                the_post();

                get_template_part( 'template-parts/content', get_post_type() );

        }
    }
发布评论

评论列表(0)

  1. 暂无评论