之前,很多朋友都问我,怎么去掉链接中的category分类标志,例如:https://www.jb51.cc/category/217.html改成https://www.jb51.cc/217.html
这样看起来链接不仅美观,还有利于收录。
但在这里给大家奉劝一句:如果你是新站,我建议你马上就改掉。但如果你是老站,就最好别改了。
把代码放在主题文件functions.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(); } // 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; }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。