2017-08-15 152 views
0

我有這個網址:WordPress的URL重寫規則

example.com/products-and-services/?catID=5 

我想讓它像這樣:

example.com/products-and-services/5 

基本上我刪除?catID=

這裏是我的代碼以add_rewrite_rule這到目前爲止我還沒有運氣:

function custom_rewrite_products() { 
    add_rewrite_rule('products-and-services/([^/]+)/?$', 
    'index.php?pagename=products-and-services&catID=$matches[1]', 'top'); 
} 

add_action('init', 'custom_rewrite_products') 

有什麼建議嗎?

回答

0

試試這個:

function custom_rewrite_products() { 
    add_rewrite_rule(
     '^products-and-services/([^/]*)/?$', 
     'index.php?pagename=products-and-services&catID=$matches[1]', 
     'top' 
    ); 
} 
add_action('init', 'custom_rewrite_products'); 

更多細節在這裏:https://wordpress.stackexchange.com/questions/250837/understanding-add-rewrite-rule

+0

我試過了,沒有工作 –

+0

@ShahGhafoori並以下網址工作= example.com/products-and-services/5/?請確保包含尾部斜線。 – zeropsi

0

用這個。

function custom_rewrite_tags() { 
    add_rewrite_tag('%catID%', '([^&]+)'); 
} 
add_action('init', 'custom_rewrite_tags', 10, 0); 

function custom_rewrite_products() { 
    add_rewrite_rule('^products-and-services/([\w+]*)/', 'index.php/?pagename=products-and-services&catID=$matches[1]', 'top'); 
} 

add_action('init', 'custom_rewrite_products'); 
+0

它試過了,它沒有工作 –