WordPress的URL重写规则

问题描述:

我有这个网址: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') 

有什么建议吗?

试试这个:

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

用这个。

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

它试过了,它没有工作 –