0

如何使用放置在我网站中的搜索框搜索我的 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 和剩余页面。

提前致谢

4

2 回答 2

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'); ?>
于 2014-12-18T13:27:34.750 回答
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在您的文件中添加上述代码。

因此,现在当您键入内容时,它会将其重定向到该页面。

注意:如果您键入与已发布帖子相关的内容,它将重定向到该帖子,而不是您输入的内容。

例如:如果我在搜索框中输入,那么hello它会将您重定向到hello world(这是安装 wordpress 期间出现的默认帖子)。

这是给你的。

于 2014-12-18T12:02:03.780 回答