我有一個WordPress的博客與永久鏈接。我創建了一個顯示產品表的靜態頁面(www.abc.xx/database /)。如果用戶點擊其中一個產品,它將打開一個名爲'product'的新靜態頁面,該頁面通過其數據庫中的id檢索並顯示產品信息。 URL是www.abc.xx/database/product?id = 1。在靜態頁面上的URL重寫
但我想有一個像www.abc.xx/database/product/my-product而不是www.abc.xx/database/product?id = 1的URL?有人能解釋它是如何工作的嗎?
UPDATE:
我打得有點與周圍貼在博客上的一些解決方案。我將下面的代碼片段添加到我的functions.php文件中。
add_filter('rewrite_rules_array', 'add_rewrite_rules');
function add_rewrite_rules($aRules) {
$aNewRules = array('database/product/([^/]*)/?$' => $wp_rewrite->index .'index.php?pagename=product&product-name=$matches[1]');
$aRules = $aNewRules + $aRules;
return $aRules;
}
add_filter('query_vars', 'add_query_vars');
function add_query_vars($aVars) {
$aVars[] = 'product-name';
return $aVars;
}
在產品頁面的網站,我添加
if(isset($wp_query->query_vars['product-name'])) {
echo urldecode($wp_query->query_vars['product-name']);
}
現在,如果我叫www.abc.xx /數據庫/產品/我的產品,該產品頁面打開,但「我的產品」未發貨。 「product-name」var沒有設置。任何提示?
我已經想你的建議之前,我在這裏張貼了我的問題。不幸的是,該程序不適用於Wordpress。 – nullpointr
@nullpointr看看這個[在WordPress中添加mod_rewrite規則](http://kovshenin.com/2009/adding-mod_rewrite-rules-to-htaccess-in-wordpress/)有幫助。 –
謝謝你的提示。它現在部分工作。我更新了第一部分,我描述了我的新問題,其中我的參數不通過頁面傳送。任何提示? – nullpointr