Thursday, May 25, 2023

Search functionality in wordpress custom theme

Hello. Today I saw you how to make search functionality in custom theme of wordpress. 

Put this search form in your search area.


<div class="sidebar__search">
<form role="search" method="get" id="searchform" action="<?php echo esc_url( home_url( '/' ) ); ?>" class="sidebar__search-form">
<div>
<input type="text" value="<?php echo get_search_query(); ?>" name="s" id="s" placeholder="Keywrord.." />
<input type="submit" id="searchsubmit" value="Search" />
</div>
</form>
</div>


search.php

<?php
/**
 * The template for displaying search results pages
 *
 * @link https://developer.wordpress.org/themes/basics/template-hierarchy/#search-result
 *
 * @package WordPress
 * @subpackage Twenty_Seventeen
 * @since 1.0
 * @version 1.0
 */
get_header(); ?>
<section class="blog-details blog_single">
    <div class="container">
        <div class="row">
            <div class="col-xl-8 col-lg-7 searchpage">
                <p>You searched for : <?php printf( __( '%s', 'immigration' ), '<span>' . get_search_query() . '</span>' ); ?></p>
<?php
                        global $query_string;
                        $query_args = explode("&", $query_string);
                        $search_query = array();
                        foreach($query_args as $key => $string) {
                          $query_split = explode("=", $string);
                          $search_query[$query_split[0]] = urldecode($query_split[1]);
                        } // foreach
                        $the_query = new WP_Query($search_query);
                        if ( $the_query->have_posts() ) {
                        ?>
                        <!-- the loop -->
                        <ol>    
                        <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
                            <li>
                                <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                            </li>   
                        <?php endwhile; ?>
                        </ol>
                        <!-- end of the loop -->
                        <?php wp_reset_postdata(); ?>
                    <?php } else {  ?>
                        <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
                    <?php } 
                    ?>
                </div>
                <div class="col-xl-4 col-lg-5 blog-sidebar__col">
                    <div class="blog-sidebar__right">
                        <?php include ("sidebar-blog.php"); ?>
                    </div>
                </div>
        </div>
    </div>
</section>
 <?php get_footer(); ?>




No comments:

Post a Comment