博客又做了大的改动

贵在折腾,我的博客又做的大的改动,我把url中的category去掉了。文章名称由原来的数字改为了数子+.html。这样做还是老话,对收录对SEO不利,但我不图这些了。

去掉链接中的“category”
在主题中修改Functions.php文件,加上以下代码:

// 去掉固定链接中的/category/
function top_level_cats_activate()
{
global $wp_rewrite;
$wp_rewrite->flush_rules();
}

function top_level_cats_deactivate()
{
remove_filter('category_rewrite_rules', 'top_level_cats_category_rewrite_rules');
remove_filter('generate_rewrite_rules', 'top_level_cats_generate_rewrite_rules');
remove_filter('category_link', 'top_level_cats_category_link');

global $wp_rewrite;
$wp_rewrite->flush_rules();
}

function top_level_cats_generate_rewrite_rules($wp_rewrite)
{
global $clean_category_rewrites;
$wp_rewrite->rules = $wp_rewrite->rules + $clean_category_rewrites;
}

function top_level_cats_category_rewrite_rules($category_rewrite)
{
global $clean_category_rewrites;

global $wp_rewrite;
$wp_rewrite->use_verbose_page_rules = true;

while (list($k, $v) = each($category_rewrite)) {
$new_k = top_level_cats_remove_cat_base($k);
$clean_category_rewrites[$new_k] = $v;
}

return $category_rewrite;
}

function top_level_cats_category_link($cat_link, $cat_id)
{
return top_level_cats_remove_cat_base($cat_link);
}

function top_level_cats_remove_cat_base($link)
{
$category_base = get_option('category_base');

if ($category_base == '')
$category_base = 'category';

if (substr($category_base, 0, 1) == '/')
$category_base = substr($category_base, 1);

$category_base .= '/';

return preg_replace('|' . $category_base . '|', '', $link, 1);
}
add_filter('category_rewrite_rules', 'top_level_cats_category_rewrite_rules');
add_filter('generate_rewrite_rules', 'top_level_cats_generate_rewrite_rules');
add_filter('category_link', 'top_level_cats_category_link', 10, 2);

global $clean_category_rewrites, $clean_rewrites;
$clean_category_rewrites = array();

您可以选择一种方式赞助本站

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: