2010-09-17 55 views
1

我有我的WordPress 3.1的网站中添加的,在我的sidebar.php文件的底部下面的代码:WordPress的搜索查询

<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

感谢。

回答

0

-1。对WP网站的所有请求都会转到一个基于特定条件的路由页面。因此,当执行搜索时,它知道它是搜索并且指向search.php页面。

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

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

0
  1. WordPress的认定请求是搜索,如果它看到一个“S” 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; 
        } 
    } 
    
+1

在load_template()之后退出并不好,因为加载模板后可能无法使用钩子。 – rbncha 2012-08-10 11:57:48

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; 
} 
+0

如果这是你的正确答案,你应该接受答案,否? – rbncha 2012-08-28 11:19:07