如何在WordPress中设置重写规则



如何使用以下URL"https://mywebsite.com/product-detail/model=MH-TOOL-152&isToolId=1"在WordPress中设置重写规则这是扫描二维码后重定向的链接。我应该得到参数model的值,因为这是产品SKU的基础,并且会将我的短代码放在产品详细信息页面下。

我在下面尝试过这个,它将我重定向到主页。我正在尝试设置它只会在产品详细信息页面上显示它。

function mywebsite_rewrite_rule() {
add_rewrite_rule('^product-detail/([^/]*)/?', 'index.php/model=$matches[1]', 'top');
}
add_action('init', 'mywebsite_rewrite_rule', 10, 0);

我已经了解了如何在WP中使用重写规则。这就是我一直在做的。

// to get the model variable
add_filter('query_vars', 'add_model_var_prodDetail', 10, 1);
function add_model_var_prodDetail($vars_sku){
$vars_sku[] = 'model';
return $vars_sku;
}
//rewrite the URL on the specific page product-detail
add_action('init', 'rewrite_rule_productDetail', 10, 0);
function rewrite_rule_productDetail() {
add_rewrite_rule('^product-detail/([^/]*)/?','index.php?post_type=page&pagename=product-detail&model=$matches[1]','top');
add_rewrite_tag( '%model%', '(.+)' );
}

然后使用get_query_var检索模型

最新更新