免插件实现No Category Base效果:去除分类标志
wordpress吧
全部回复
仅看楼主
level 14
  WordPress对于分类目录的链接是这样定义的:http://xx.com/category/diary,其中的category部分难免觉得有一些多余,且百度等搜索引擎也会将这样的链接划定至不友好的区域。如果你对博客的SEO方面特别重视,不妨试一下!
  加入该代码至主题文件functions.php:
<?php
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');
function no_category_base_refresh_rules() {
global $wp_rewrite;
$wp_rewrite -> flush_rules();
}
add_action('init', 'no_category_base_permastruct');
function no_category_base_permastruct() {
global $wp_rewrite, $wp_version;
if (version_compare($wp_version, '3.4', '<')) {
$wp_rewrite -> extra_permastructs['category'][0] = '%category%';
} else {
$wp_rewrite -> extra_permastructs['category']['struct'] = '%category%';
}
}
add_filter('category_rewrite_rules', 'no_category_base_rewrite_rules');
function no_category_base_rewrite_rules($category_rewrite) {
$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]';
}
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]';
return $category_rewrite;
}
add_filter('query_vars', 'no_category_base_query_vars');
function no_category_base_query_vars($public_query_vars) {
$public_query_vars[] = 'category_redirect';
return $public_query_vars;
}
add_filter('request', 'no_category_base_request');
function no_category_base_request($query_vars) {
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;
} ?>
2014年06月01日 03点06分 1
level 7
沙发。
2014年06月01日 04点06分 2
level 14
小P,我装了这个插件,现在可以直接卸载掉然后再用你这个方法吗[呵呵]
2014年06月01日 05点06分 3
=w=试一下应该可以
2014年06月01日 05点06分
level 11
其实一样的 WP大学有篇文章说过 这样和插件是一样的执行效率
2014年06月01日 15点06分 4
为主题集成提供方便
2014年06月01日 16点06分
回复 PC丶爱好者 :[乖]嗯,这到是不错,我理解为了提高执行效率了。
2014年06月01日 16点06分
level 13
我觉得这个还是用插件好些,主题集成的话,有些人不愿意去掉这个category ,插件会更灵活一些
2014年06月02日 03点06分 5
level 13
这样子看起来好高端呢
2014年06月02日 15点06分 6
level 1
有什么差别。
2014年06月04日 06点06分 7
1