Правила перезаписи 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')

Какие-либо предложения?

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

Другие решения

Использовать этот.

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

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector