这个我敢肯定是不少用wordpress的朋友需要的!
大家再用上wordpress后,肯定会把分类目录地址中“category”给去掉;
而且选择用WP No Category Base插件去除“category”的朋友应该有95%++
反正我是属于上面之一! ihuan.me 幻杀博客 就是一直在用WP No Category Base插件 这个插件有个好处就是301重定向啦。这个也有哦
昨天一不小心在别人站上发现这非插件实现去掉category的方法,测试了下也给分享下。

add_action('load-themes.php', 'no_category_base_refresh_rules');
add_action('created_category','no_category_base_refresh_rules');
add_action('edited_category','no_category_base_refresh_rules');
add_action('delete_category','no_category_base_refresh_rules');
functionno_category_base_refresh_rules(){
global$wp_rewrite;
$wp_rewrite->flush_rules();
}
// register_deactivation_hook(__FILE__, 'no_category_base_deactivate');
// function no_category_base_deactivate() {
// remove_filter('category_rewrite_rules', 'no_category_base_rewrite_rules');
// // We don't want to insert our custom rules again
// no_category_base_refresh_rules();
// }
// Remove category base
add_action('init','no_category_base_permastruct');
functionno_category_base_permastruct(){
global$wp_rewrite,$wp_version;
if(version_compare($wp_version,'3.4','<')){
// For pre-3.4 support
$wp_rewrite->extra_permastructs['category'][0]='%category%';
}else{
$wp_rewrite->extra_permastructs['category']['struct']='%category%';
}
}
// Add our custom category rewrite rules
add_filter('category_rewrite_rules','no_category_base_rewrite_rules');
functionno_category_base_rewrite_rules($category_rewrite){
//var_dump($category_rewrite); // For Debugging
$category_rewrite=array();
$categories=get_categories(array('hide_empty'=>false));
foreach($categories as$category){
$category_nicename=$category->slug;
if($category->parent==$category->cat_ID)// recursive recursion
$category->parent=0;
elseif($category->parent!=0)
$category_nicename=get_category_parents($category->parent,false,'/',true).$category_nicename;
$category_rewrite['('.$category_nicename.')/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$']='index.php?category_name=$matches[1]&feed=$matches[2]';
$category_rewrite['('.$category_nicename.')/page/?([0-9]{1,})/?$']='index.php?category_name=$matches[1]&paged=$matches[2]';
$category_rewrite['('.$category_nicename.')/?$']='index.php?category_name=$matches[1]';
}
// Redirect support from Old Category Base
global$wp_rewrite;
$old_category_base=get_option('category_base')?get_option('category_base'):'category';
$old_category_base=trim($old_category_base,'/');
$category_rewrite[$old_category_base.'/(.*)$']='index.php?category_redirect=$matches[1]';
//var_dump($category_rewrite); // For Debugging
return$category_rewrite;
}
// Add 'category_redirect' query variable
add_filter('query_vars','no_category_base_query_vars');
functionno_category_base_query_vars($public_query_vars){
$public_query_vars[]='category_redirect';
return$public_query_vars;
}
// Redirect if 'category_redirect' is set
add_filter('request','no_category_base_request');
functionno_category_base_request($query_vars){
//print_r($query_vars); // For Debugging
if(isset($query_vars['category_redirect'])){
$catlink=trailingslashit(get_option('home')).user_trailingslashit($query_vars['category_redirect'],'category');
status_header(301);
header("Location: $catlink");
exit();
}
return$query_vars;
}
如果你已经在用WP No Category Base插件的话就别用以上方法了,那个插件用了一次的话就必须得一直用下去;
不然文章页会无法访问,当然你也可以想办法把这个插件彻底卸载了,再用上面的方法!
还有就是把WP No Category Base插件关闭自然也是没用的....后悔啊!!!
楼上的回复!我也是醉了!
2015年5月5日 06:49
哪个楼上?
2015年5月5日 12:46不错的方法
2015年4月2日 09:15应该去掉 category,看起来太不舒服了。
2015年3月9日 21:50每次我都是用插件,代码盲啊!多谢博主分享!
2015年2月16日 16:17把插件直接删除,添加那些代码,然后在固定连接设置里面重新设置一下,文章页就可以打开了。
2015年1月10日 14:45不重新设置固定链接,文章页是打不开的
2015年1月10日 14:45换个简短的单词就好了 不喜欢直接去掉、
2014年10月22日 22:02目录下我不喜欢有别的
2014年10月23日 12:17最好别用插件。。。
2014年10月21日 09:04不加倍
2014年10月17日 22:45不加倍
2014年10月18日 11:58伪地址的会有吧,坦白说没看太懂
2014年10月17日 22:44抢地主
2014年10月17日 19:46我抢
2014年10月17日 20:32