简体   繁体   中英

WordPress URL Rewrite Rules

I have this URL:

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

I want to make it like this:

example.com/products-and-services/5

Basically I am removing ?catID=

Here is my code with add_rewrite_rule which I have no luck so far:

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

Any suggestions?

Give this a try:

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

More details here: https://wordpress.stackexchange.com/questions/250837/understanding-add-rewrite-rule

Use This.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM