[英]Rewrite rules for URL prefixes
I am rewriting some URL prefixes (ie category prefix, tag prefix or custom taxonomy prefix) in my Wordpress plugin. 我在Wordpress插件中重写了一些URL前缀(即类别前缀,标签前缀或自定义分类前缀)。 I am using Wordpress' rewrite API : 我正在使用Wordpress的重写API :
add_filter('rewrite_rules_array','wp_insertMyRewriteRules');
add_filter('init','flushRules');
// Remember to flush_rules() when adding rules
function flushRules(){
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
// Adding a new rule
function wp_insertMyRewriteRules($rules)
{
$newrules = array();
$newrules['kategorie/(.+?)/?$'] = 'index.php?category_name=$matches[1]';
return $newrules + $rules;
}
It is working, but the system URLs on site have not changed, and WP is using the old prefix 'category' from the database. 它正在运行,但是站点上的系统URL尚未更改,并且WP使用的是数据库中的旧前缀“类别”。 How do I rewrite those URLs as well? 我也该如何重写这些URL?
If you want to change the permalink structure for all categories, then you can configure that at Settings -> Permalinks -> Category base . 如果要更改所有类别的永久链接结构,则可以在设置->永久链接->类别库中进行配置 。
If you want to change the link for only one or a few categories, you can use the WordPress category_link filter: 如果您只想更改一个或几个类别的链接,则可以使用WordPress category_link过滤器:
function update_permalink( $permalink, $cat_id ) {
// If $cat_id is category we want to change, modify $permalink
return $permalink;
}
add_filter( 'category_link', 'update_permalink', 11, 2 );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.