WordPress 重写规则
Wordpress re write rule
假设我有一个 URI 那是 portfolio_categories/guttersspouting/
我怎样才能重写一个函数来改变它"products"
ok,按照要求,这与 uri "product" 完全匹配。如果将代码发布给第三方,这是不可取的,就像他们创建一个名为 product 的页面一样,它将遵循此规则。
顺便说一句,我假设你的意思是 post_type = page(也适用于 posts,请参阅 add_rewrite_rule 中的 post_id=
?你需要更改此到您要重定向到的页面的 post ID。
add_action('init', 'new_rewrite_rule');
function new_rewrite_rule(){
// matches product exactly, product/aproduct will fail, etc
// you need to add your own post_id into the function below.
add_rewrite_rule('^product$','index.php?page_id=12','top');
}
您需要刷新重写规则才能正常工作(转到永久链接设置页面并点击保存按钮)
flush 重写规则的代码版本,在 wp_loaded 上使用它 运行 不是一个好主意,实际上应该在激活挂钩上但是为了测试它会做:
function new_flush_rewrite_rules() {
flush_rewrite_rules();
}
add_action( 'wp_loaded', 'new_flush_rewrite_rules' );
请注意,您也可以在 post 编辑屏幕中手动执行此操作。
假设我有一个 URI 那是 portfolio_categories/guttersspouting/
我怎样才能重写一个函数来改变它"products"
ok,按照要求,这与 uri "product" 完全匹配。如果将代码发布给第三方,这是不可取的,就像他们创建一个名为 product 的页面一样,它将遵循此规则。
顺便说一句,我假设你的意思是 post_type = page(也适用于 posts,请参阅 add_rewrite_rule 中的 post_id=
?你需要更改此到您要重定向到的页面的 post ID。
add_action('init', 'new_rewrite_rule');
function new_rewrite_rule(){
// matches product exactly, product/aproduct will fail, etc
// you need to add your own post_id into the function below.
add_rewrite_rule('^product$','index.php?page_id=12','top');
}
您需要刷新重写规则才能正常工作(转到永久链接设置页面并点击保存按钮)
flush 重写规则的代码版本,在 wp_loaded 上使用它 运行 不是一个好主意,实际上应该在激活挂钩上但是为了测试它会做:
function new_flush_rewrite_rules() {
flush_rewrite_rules();
}
add_action( 'wp_loaded', 'new_flush_rewrite_rules' );
请注意,您也可以在 post 编辑屏幕中手动执行此操作。