搜索功能是任何網站都必須的功能,通過搜索功能,用戶可以快速的在我們的網站上找到自己需要的內容。在wordpress模板制作教程中,搜索功能也需要制作出來,這就是搜索功能模板(search.php)。
- 原文來源:詳情

模板制作步驟
第一步:復制分類目錄模板(archive.php),直接重命名為search.php。用于顯示用戶的搜索結果內容;
第二步:在主題文件夾里新建一個searchform.php,放入以下代碼,這是一個獨立的搜索框的代碼。
<form method="get" id="searchform" action="?php echo esc_url( home_url() ) ?>">
<input class="searchInput" type="text" value="輸入關鍵字" name="s" id="s"/>
<input type="submit" value="搜 索" onClick="if(document.forms['search'].searchinput.value=='- Search -')document.forms['search'].searchinput.value='';" alt="Search" />
</form>
<script type="text/javascript">
$(document).ready(function(){
// 當鼠標聚焦在搜索框
$('#s').focus(
function() {
if($(this).val() == '輸入關鍵字') {
$(this).val('').css({color:"#454545"});
}
}
// 當鼠標在搜索框失去焦點
).blur(
function(){
if($(this).val() == '') {
$(this).val('輸入關鍵字').css({color:"#333333"});
}
}
);
});
</script>
第三步:在需要顯示搜索框的位置,使用以下的代碼去調用這個搜索框。
<?php include (TEMPLATEPATH . '/searchform.php'); ?>
第四步:網站搜索框可以被任何其它模板調用,用戶通過搜索框搜索某個詞時,搜索結果就會通過search.php模板展示出來。
同過此代碼,我開發了這款有趣的wordpress插件:
如果您需要更加強大的搜索功能,可以參考這款插件: