之前,很多朋友都问我,怎么去掉链接中的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 baseadd_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 rulesadd_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 variableadd_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 setadd_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;}总结
以上是内存溢出为你收集整理的WordPress去掉链接中category分类的方法全部内容,希望文章能够帮你解决WordPress去掉链接中category分类的方法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)