2
我設置了一個自定義搜索表單來搜索和顯示我的自定義帖子類型的結果。形式是:WordPress的自定義帖子類型搜索結果頁面
<form class="search-form" action="<?php echo home_url('/'); ?>">
<input type="search" name="s" placeholder="Search…">
<input type="hidden" name="post_type" value="resource-library">
<input type="submit" value="Search">
</form>
在我的search.php我想直接鄉親稱爲搜索資源library.php自定義搜索PHP文件如果內容類型爲資源庫。所以我把這個在我的search.php頁面的頂部:
<?php
// store the post type from the URL string
$post_type = $_GET['post_type'];
// check to see if there was a post type in the
// URL string and if a results template for that
// post type actually exists
if (isset($post_type) && locate_template('search-' . $post_type . '.php')) {
// if so, load that template
get_template_part('search', $post_type);
// and then exit out
exit;
}
?>
我得到什麼,這是做什麼,但我似乎無法存儲在搜索表單的HTML理應設定的URL字符串崗位類型。
任何想法?
你是什麼意思「存儲」郵政類型?你的意思是這一行:'$ post_type = $ _GET ['post_type']'是不是設置帖子類型?或者是其他東西? –
正確。點擊搜索表單中的提交按鈕後,我無法將post_type追加到網址。我需要它在url中傳遞一個?post_type = research-library。 –
對不起,這更令人困惑。 '$ post_type'是否在你的代碼中得到正確的值,或者不是?如果沒有,你嘗試'$ post_type = $ _POST ['post_type'];' –