如何使用搜索框搜索我的WordPress网站页面

时间:2014-12-18 11:33:05

标签: wordpress search-box

如何使用我放置在我网站中的搜索框搜索我的WordPress网站页面? 目前我正在使用此代码。

<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>

我使用了name =“s”,但没有正确搜索。我希望如果用户右侧page1名称搜索框将其带到siteurl / page1,那么对于page2和剩余页面。

提前致谢

2 个答案:

答案 0 :(得分:0)

试试这个:

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

functions.php文件中添加以上代码。

所以现在当你输入内容时,它会将它重定向到那个页面。

注意:如果您输入与发布的帖子相关的内容,它会重定向到 POST ,而不是您输入的内容。< / p>

对于示例:如果我在搜索框中输入hello,则会将您重定向到hello world(这是在安装wordpress期间出现的默认帖子)。< / p>

这是适合你的。

答案 1 :(得分:0)

<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'); ?>
相关问题