2010-09-17 71 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