2013-04-11 101 views
4

我有一個名爲「人」與發生在一個叫做變量名的自定義模板頁:什麼是使用add_rewrite_rule刪除(問號)的正確方法?

example.com/people/?name=FirstLast

我想是這樣的:

example.com/people/FristLast

我知道我需要使用add_rewrite_rule和add_rewrite_tag電話,但我不知道如何實現我想要的結果。我試過,但我得到一個PHP的錯誤:

add_rewrite_rule('^people/([^/]*)/?','index.php?name=$matches[1]','top'); 
add_rewrite_tag('%name%','([^&]+)'); 

我得到的錯誤是:「致命錯誤:調用一個成員函數add_rule()一個非對象」

我想我在正確的道路上。我對WP非常瞭解,但這是我第一次嘗試重寫規則。

謝謝!

回答

6

嘗試

add_action('init', 'addMyRules'); 
function addMyRules(){ 
    add_rewrite_rule('^people/([^/]*)/?','index.php?name=$matches[1]','top'); 
    add_rewrite_tag('%name%','([^&]+)'); 
} 
0

你應該叫add_rewrite_rule在行動掛鉤如下:

add_action('init', 'wpa8715_intit'); 

function wpa8715_intit() { 
    add_rewrite_rule('^people/([^/]*)/?','index.php?name=$matches[1]','top'); 
    add_rewrite_tag('%name%','([^&]+)'); 
} 
相關問題