Remove Category Base from WordPress Category URL
I fished around the internet for a solution to this, tried a plugin or two to r开发者_开发问答emove the /category/ from wordpress url's.
While some of these plugins are good, the category link still display's /category/.
Also I've tried putting in ./ in the category base options in permalinks settings.
Does anyone know how I could do like a php search and replace or something like that?
A cleaner solution:
add_filter('user_trailingslashit', 'remcat_function');
function remcat_function($link) {
return str_replace("/category/", "/", $link);
}
add_action('init', 'remcat_flush_rules');
function remcat_flush_rules() {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
add_filter('generate_rewrite_rules', 'remcat_rewrite');
function remcat_rewrite($wp_rewrite) {
$new_rules = array('(.+)/page/(.+)/?' => 'index.php?category_name='.$wp_rewrite->preg_index(1).'&paged='.$wp_rewrite->preg_index(2));
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
Using WordPress 3.9.1 (latest version as of this post) I simply added a single line to my theme's functions.php
...
$wp_rewrite->add_permastruct('category_base', '%category%');
Then I opened Settings > Permalinks and hit Save. This appears to flush the permalink cache and makes it work.
http://wordpress.org/extend/plugins/wp-no-category-base/ and it doesn't alter permalinks, so removing it reverts the structure with no problem. And you don't have to alter core files.
https://wordpress.org/plugins/no-category-base-wpml/ is a plugin that solves the problem and works with current versions of WordPress.
精彩评论