Есть 2 таксономии product_brand - бренды и product_cat - категории.
Для того чтобы показать товары категории определенного бренда используется ссылки вида:
/product_brand/brand_name/?attributes[product_cat]=product_cat_id
Как преобразовать его в:
/product_brand/brand_name/product_cat_name
Я так понял мне нужно использовать функцию add_rewrite_rule(), но не могу понять как заставить ее работать.
function custom_rewrite_rules() {
add_rewrite_rule('^product_brand/(.*)/(.*)?', 'index.php?&attributes[product_cat]=$matches[1]', 'top');
}
add_action('init', 'custom_rewrite_rules');
Помогло следующее:
function custom_rewrite_rules() {
// порядок имеет значение
// для ссылок типа: product_brand/brand_name/page
add_rewrite_rule( '^product_brand/([^/]+)/page/([0-9]{1,})/?', 'index.php?product_brand=$matches[1]&page=$matches[2]', 'top' );
// для ссылок типа: product_brand/brand_name/category_name/page
add_rewrite_rule( '^product_brand/([^/]+)/([^/]+)/page/([0-9]{1,})/?', 'index.php?product_brand=$matches[1]&product_cat=$matches[2]&page=$matches[3]', 'top' );
// для ссылок типа: product_brand/brand_name/category_name
add_rewrite_rule('^product_brand/([^/]+)/([^/]+)?', 'index.php?product_brand=$matches[1]&product_cat=$matches[2]', 'top');
add_filter( 'query_vars', function( $vars ){
$vars[] = 'product_brand';
$vars[] = 'product_cat';
return $vars;
} );
}
add_action('init', 'custom_rewrite_rules');
Для того чтобы заставить WP переключать страницы, дополнительно пришлось добавить это
add_action( 'pre_get_posts', function() {
$page = get_query_var('page');
$query->set( 'paged', $page );
return $query;
} );
Продвижение своими сайтами как стратегия роста и независимости