How do I search my WordPress site pages with search box

70 Views Asked by At

How I search my WordPress site pages with search box I placed in my site? Currently I am using this code.

<form id="searchform" method="get" action="<?php bloginfo('siteurl')?>/" style="margin-left: -43px;">
    <input type="text" name="" id="s" class="textbox" placeholder="Search" required/>
    <input id="btnSearch" type="submit" name="submit" value="<?php _e('Search'); ?>" />
</form>

I used name="s" but it not do search properly. I want that if user right page1 name search box take it to siteurl/page1 and so for page2 and remaining page.

Thanks in advance

2

There are 2 best solutions below

0
On

Try this :

    function redirect_search() {
        if (is_search() && !empty($_GET['s'])) {
            wp_redirect(home_url("/").urlencode($_GET['s']));
            exit();
        }
    }
    add_action('template_redirect', 'redirect_search' );

Add above code in your functions.php file.

So now when you type something, it will redirect it to that page.

NOTE:If you type something related to the published post, it will redirect to that POST instead of what have you entered.

For example : If I type hello in search box, then it will redirect you to hello world(which is default post that comes during installation of wordpress).

This is what have for you.

0
On
<div id="content" class="textbox <?php echo of_get_option('blog_sidebar_pos') ?>">
 <?php _e('Search for:',''); ?> "<?php the_search_query(); ?>"

    <?php 

        if (have_posts()) : while (have_posts()) : the_post(); 

                $format = get_post_format();
                get_template_part( 'include/'.$format );

                if($format == '')
                get_template_part( 'include/standard' );

         endwhile; else:

         ?>

            <?php echo '<p><strong>' . __('There has been an error.', '') . '</strong></p>'; ?>
            <p> <a href="<?php bloginfo('url'); ?>/" title="<?php bloginfo('description'); ?>"><?php _e('return to home page', ''); ?></a> <?php _e('use the search .', ''); ?></p>
            <?php get_search_form(); ?>
        </div><!--no-results-->

    <?php endif; ?>

  <?php get_template_part('include/post'); ?>