今天要介紹的是去除鏈接地址中存在Category字樣的問題,百度貌似對三級目錄的收錄不喜,它的功能就是刪除分類目錄鏈接中那有些多余的 /category/ 字樣。若單單為了優化網站而再多裝一個插件實在不值,不如……把它集成到主題中?
精簡代碼版
修改functions.php
代碼是從一個插件里面的一大串代碼里面扣出來的有用的部分,插件什么的能省一個就省一個吧!至于把舊鏈接自動跳轉,請看下面的301設置。
在主題的functions.php中,添加以下代碼,保存即可。
//去除分類目錄鏈接中的category
add_action('init', 'no_category_base_permastruct');
function no_category_base_permastruct() {
global $wp_rewrite;
$wp_rewrite -> extra_permastructs['category']['struct'] = '%category%';
}
.htaccess增加301跳轉
本站使用的是Apache主機,這里給出Apache主機的.htaccess 301跳轉的方法,打開主機根目錄下的.htaccess文件,把以下代碼加載# BEGIN WordPress前面。
RewriteEngine On
RewriteBase /
RewriteRule ^category/(.+)$ http://www.timle.cn/$1 [R=301,L]
當然,不要忘記,上述的代碼中,把網址改成你自己的網址喔。
完整代碼版
使用精簡代碼版如有問題,可采用完整代碼版。
加入該完整代碼至主題文件functions.php:
//去除鏈接地址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');
function no_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');
function no_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');
function no_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');
function no_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');
function no_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;
}
以上方法可以很好的解決鏈接地址中存在 /category/ 字樣的問題。