WordPress搜索查询

时间:2010-09-17 14:35:19

标签: wordpress wordpress-theming

我在我的WordPress 3.1网站中添加了以下代码,位于sidebar.php文件的底部:

    <div id="search_box">
      <form id="searchform" action="http://www.service.com/" method="get" role="search">
          <input id="s" type="text" name="s" class="search" value="Search site" size="19" maxlength="80" id="white_box" onfocus="if (this.value=='Search site') this.value = ''"/>
          <input id="searchsubmit" type="image" class="submit" value="submit" src="<?php bloginfo( 'template_url' ); ?>/images/search_btn.jpg" />
      </form>
    </div>

由于我自己编写了这个搜索过程,当我在搜索文本框中放置一些文本并按下“搜索”按钮时,看起来好像是在我的主题中调用页面search.php并显示结果

问题:

1)当我按下“搜索”按钮关闭并调用search.php时,在哪里/如何知道?

2)如果可能的话,如何更改它来调用不同的php文件而不是search.php

感谢。

3 个答案:

答案 0 :(得分:1)

使用模板过滤器

add_filter( 'template_include', 'template_include', 10 );

并将模板更改为

function template_include($template)
{

    if(your condition here){
        $template = get_template_directory().'/your-template.php';
    }

    return $template;
}

答案 1 :(得分:0)

-1。对WP站点的所有请求都转到单个页面,该页面根据特定条件对它们进行路由。因此,当执行搜索时,它知道它是搜索并指向search.php页面。

具体来说,它会转到加载wp-blog-header.php。

的index.php

-2:你需要知道的一切应该就在这里:http://codex.wordpress.org/Creating_a_Search_Page#Using_the_page.php

答案 2 :(得分:0)

  1. Wordpress假设请求是一个搜索,如果它看到'''GET变量。

  2. 您可以加入template_redirect操作(在主题的functions.php文件中)并加载不同的模板,如下所示:

    add_action('template_redirect', 'my_template_select');
    function my_template_select() {
      if (is_search()) {
        load_template(TEMPLATEPATH . '/foobar.php');
        exit;
      } 
    }
    
相关问题