WordPress中去掉固定链接中的category

WordPress中去掉固定链接中的category,第1张

概述wordpress默认情况下设置固定链接,分类页面是有category路径的。出于seo优化的考虑,网站的链接层次多,不利于蜘蛛的抓取,也就是说链接越简洁越好,链接的路径越少越好。在百度优化指南中,百度一直是建议站长们使用浅表的层次结构。下面我们讲解一下WordPress建站中去掉固定链接中的category的方法。

wordpress默认情况下设置固定链接,分类页面是有category路径的。出于SEO优化的考虑,网站的链接层次多,不利于蜘蛛的抓取,也就是说链接越简洁越好,链接的路径越少越好。在百度优化指南中,百度一直是建议站长们使用浅表的层次结构。

下面我们讲解一下href="https://www.jb51.cc/">wordpress建站中去掉固定链接中的category的方法。

方法1:切换到主题目录,打开functions.PHP文件,添加如下代码,然后到wordpress后台中设置的地方,点击固定链接,保存即可生效。

extra_permastructs['category']['struct'] = '%category%';}

方法2:基于插件WP No category Base上的代码,切换到主题目录,打开functions.PHP文件,添加如下代码,然后到wordpress后台中设置的地方,点击固定链接,保存即可生效。

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;}nicenamenicenamenicenamebase

方法3:基于伪静态,主机使用linux系统的用户,可以将带有category的链接重定向至没有它的链接,通过编写.htaccess的301重定向规则

总结

以上是内存溢出为你收集整理的WordPress中去掉固定链接中的category全部内容,希望文章能够帮你解决WordPress中去掉固定链接中的category所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/zz/1022952.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存