WordPress URL重写规则

时间:2017-08-15 22:01:15

标签: php wordpress

我有这个网址:

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')

有什么建议吗?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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');