首页 > CMS > Wordpress > 正文

Wordpress如何将列表URL结尾添加”/” 如何自定义类型的URL的固

2024-09-07 00:52:17
字体:大 中 小
来源:转载
供稿:网友

Wordpress如何将列表URL结尾添加”/” 如何自定义类型的URL的固定链接

我们来分享两篇关于Wordpress的URL优化技巧的教程,将分类、标签(Tag)等列表URL结尾添加”/”斜线呢,添加自定义类型的URL的固定链接。

Wordpress如何将分类、标签(Tag)等列表URL结尾添加”/”斜线呢?

WordPress强大之处就是在于,我们能想到的已经实现了,只是我们不知道而已。

网上有很多解决方案,基本归类为:

1. 通过Rewrite Rule 301跳转实现(如果是已经被收录的URL可以的,但刚开始时不建议使用)

2. 通过修改wp-includes/canonical.php实现(升级时会被覆盖,不建议)

3. 通过插件来实现

下面就说一下插件方法:

插件 Permalink Trailing Slash Fixer 已经实现这个功能。

下载后查看代码,你会发现只有几行而已,而且内部调用的是Wordpress自带的方法实现的,并且不会出现301跳转,个人认为非常理想的解决方案.

代码如下:

  1. /** 
  2.  * Public staff only. 
  3.  */ 
  4. if (is_admin()) return; 
  5.  
  6. $permalink_structure = get_option('permalink_structure'); 
  7. if (!$permalink_structure || '/' === substr($permalink_structure, -1)) 
  8.     return; 
  9.  
  10. add_filter('user_trailingslashit', 'ppm_fixe_trailingslash', 10, 2); 
  11.  
  12. /** 
  13.  * Appends a trailing slash if it's missing in the permalink structure. 
  14.  * 
  15.  * Conditionally adds a trailing slash if the url type is not "single". 
  16.  * 
  17.  * @param string $url A URL with or without a trailing slash. 
  18.  * @param string $type The type of URL being considered (e.g. single, category, etc). 
  19.  * @return string The URL with the trailing slash fixed. 
  20.  */ 
  21. function ppm_fixe_trailingslash($url, $type) 
  22. { 
  23.     if ('single' === $type) 
  24.         return $url; 
  25.  
  26.     return trailingslashit($url); 
  27. } 
  28.  
  29. /** 
  30.  * Public staff only. 
  31.  */ 
  32. if (is_admin()) return; 
  33.  
  34. $permalink_structure = get_option('permalink_structure'); 
  35. if (!$permalink_structure || '/' === substr($permalink_structure, -1)) 
  36.     return; 
  37.  
  38. add_filter('user_trailingslashit', 'ppm_fixe_trailingslash', 10, 2); 
  39.  
  40. /** 
  41.  * Appends a trailing slash if it's missing in the permalink structure. 
  42.  * 
  43.  * Conditionally adds a trailing slash if the url type is not "single". 
  44.  * 
  45.  * @param string $url A URL with or without a trailing slash. 
  46.  * @param string $type The type of URL being considered (e.g. single, category, etc). 
  47.  * @return string The URL with the trailing slash fixed. 
  48.  */ 
  49. function ppm_fixe_trailingslash($url, $type) 
  50. { //Vevb.com 
  51.     if ('single' === $type) 
  52.         return $url; 
  53.  
  54.     return trailingslashit($url); 
  55. } 

是不是很简洁…..

如果你是使用的多站点,而之前的站点SEO还不错,现在准备添加一新站点的话,以防相互有影响建议将上面代码微调后加入到 模板目录functoins.php中,或是使用插件,只在新的站点开户插件功能,也是一样的:

  1. if (!is_admin()) { 
  2.     function ppm_fixe_trailingslash($url, $type) 
  3.     { 
  4.         $permalink_structure = get_option('permalink_structure'); 
  5.         if (!$permalink_structure || '/' === substr($permalink_structure, -1)) 
  6.         { 
  7.         return; 
  8.         } 
  9.         if ('single' === $type || 'page' === $type) 
  10.         { 
  11.         return $url; 
  12.         } 
  13.  
  14.         return trailingslashit($url); 
  15.     } 
  16.     add_filter('user_trailingslashit', 'ppm_fixe_trailingslash', 10, 2); 
  17. } 
  18.      
  19.  if (!is_admin()) { 
  20.     function ppm_fixe_trailingslash($url, $type) 
  21.     { 
  22.         $permalink_structure = get_option('permalink_structure'); 
  23.         if (!$permalink_structure || '/' === substr($permalink_structure, -1)) 
  24.         { 
  25.         return; 
  26.         } 
  27.         if ('single' === $type || 'page' === $type) 
  28.         { 
  29.         return $url; 
  30.         } 
  31.  
  32.         return trailingslashit($url); 
  33.     } 
  34.     add_filter('user_trailingslashit', 'ppm_fixe_trailingslash', 10, 2); 
  35. } 

最后,最得要的是,让新添回报URL Rules生效:进入后台?>Setting?>Permalinks刷新即可.

Wordpress如何添加自定义类型的URL的固定链接

由于业务需要,偶尔会添加一种页面类型,而这种类型不存在于Wordpress已有的routers 固定链接中,那么就需要我们来添加一种自定义的URL固定链接.

比如:

我需要添加一种/health/A~Z/的URL规则,那么如何自定义呢?

大体实现如下:

1.固定链接URL定义为:/health/A/

2.实际URL为:index.php?category_name=&idx=

实际代码如下:

# 找到模板目录下面的functions.php 添加下面代码:

  1. function add_query_vars($aVars) { 
  2.   $aVars[] = "idx";  
  3.   return $aVars; 
  4. } 
  5. // hook add_query_vars function into query_vars 
  6. add_filter('query_vars', 'add_query_vars'); 
  7.  
  8. function add_rewrite_rules($aRules) { 
  9.   $aNewRules = array('(health)/([^/]+)/?$' => 'index.php?category_name=$matches[1]&idx=$matches[2]'); 
  10.   $aRules = $aNewRules + $aRules; 
  11.   return $aRules; 
  12. } 
  13.  
  14. // hook add_rewrite_rules function into rewrite_rules_array 
  15. add_filter('rewrite_rules_array', 'add_rewrite_rules'); 
  16.  
  17. # 找到模板目录下面的functions.php 添加下面代码 
  18. function add_query_vars($aVars) { 
  19.   $aVars[] = "idx";  
  20.   return $aVars; 
  21. } 
  22. // hook add_query_vars function into query_vars 
  23. add_filter('query_vars', 'add_query_vars'); 
  24.  
  25. function add_rewrite_rules($aRules) { 
  26.   $aNewRules = array('(health)/([^/]+)/?$' => 'index.php?category_name=$matches[1]&idx=$matches[2]'); 
  27.   $aRules = $aNewRules + $aRules; 
  28.   return $aRules; 
  29. } 
  30.  
  31. // hook add_rewrite_rules function into rewrite_rules_array 
  32. add_filter('rewrite_rules_array', 'add_rewrite_rules'); 

到这里已经实现自定义URL规则的固定链接。

那么如何获取URL中的参数呢?其实知道这是Wordpress自身的路由实现就好明白了,既然是WP的路由,那么我们就像别的框架一样使用WP的获取URL的参数的访问就可以了.

在需要获取idx参数值的寂寞中,如下代码就可以得到了:

  1. #打印所有参数 
  2. var_dump($wp_query->query_vars); 
  3. #取得idx参数和值 
  4. $idx = get_query_var('idx'); 
  5.  
  6. #打印所有参数 
  7. var_dump($wp_query->query_vars); 
  8. #取得idx参数和值 
  9. $idx = get_query_var('idx');

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表