一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

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

时间:2015-05-21 编辑:简简单单 来源:一聚教程网

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

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

网上有很多解决方案,基本归类为:
1. 通过Rewrite Rule 301跳转实现(如果是已经被收录的URL可以的,但刚开始时不建议使用)
2. 通过修改wp-includes/canonical.php实现(升级时会被覆盖,不建议)
3. 通过插件来实现

下面就说一下插件方法:
插件 Permalink Trailing Slash Fixer 已经实现这个功能。

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

代码如下:

/**
 * Public staff only.
 */
if (is_admin()) return;

$permalink_structure = get_option('permalink_structure');
if (!$permalink_structure || '/' === substr($permalink_structure, -1))
    return;

add_filter('user_trailingslashit', 'ppm_fixe_trailingslash', 10, 2);

/**
 * Appends a trailing slash if it's missing in the permalink structure.
 *
 * Conditionally adds a trailing slash if the url type is not "single".
 *
 * @param string $url A URL with or without a trailing slash.
 * @param string $type The type of URL being considered (e.g. single, category, etc).
 * @return string The URL with the trailing slash fixed.
 */
function ppm_fixe_trailingslash($url, $type)
{
    if ('single' === $type)
        return $url;

    return trailingslashit($url);
}

/**
 * Public staff only.
 */
if (is_admin()) return;
 
$permalink_structure = get_option('permalink_structure');
if (!$permalink_structure || '/' === substr($permalink_structure, -1))
    return;
 
add_filter('user_trailingslashit', 'ppm_fixe_trailingslash', 10, 2);
 
/**
 * Appends a trailing slash if it's missing in the permalink structure.
 *
 * Conditionally adds a trailing slash if the url type is not "single".
 *
 * @param string $url A URL with or without a trailing slash.
 * @param string $type The type of URL being considered (e.g. single, category, etc).
 * @return string The URL with the trailing slash fixed.
 */
function ppm_fixe_trailingslash($url, $type)
{
    if ('single' === $type)
        return $url;
 
    return trailingslashit($url);
}

是不是很简洁…..

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

 if (!is_admin()) {
    function ppm_fixe_trailingslash($url, $type)
    {
        $permalink_structure = get_option('permalink_structure');
        if (!$permalink_structure || '/' === substr($permalink_structure, -1))
        {
        return;
        }
        if ('single' === $type || 'page' === $type)
        {
        return $url;
        }

        return trailingslashit($url);
    }
    add_filter('user_trailingslashit', 'ppm_fixe_trailingslash', 10, 2);
}
    
 if (!is_admin()) {
    function ppm_fixe_trailingslash($url, $type)
    {
        $permalink_structure = get_option('permalink_structure');
        if (!$permalink_structure || '/' === substr($permalink_structure, -1))
        {
        return;
        }
        if ('single' === $type || 'page' === $type)
        {
        return $url;
        }
 
        return trailingslashit($url);
    }
    add_filter('user_trailingslashit', 'ppm_fixe_trailingslash', 10, 2);
}

最后,最得要的是,让新添回报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 添加下面代码
function add_query_vars($aVars) {
  $aVars[] = "idx";
  return $aVars;
}
// hook add_query_vars function into query_vars
add_filter('query_vars', 'add_query_vars');

function add_rewrite_rules($aRules) {
  $aNewRules = array('(health)/([^/]+)/?$' => 'index.php?category_name=$matches[1]&idx=$matches[2]');
  $aRules = $aNewRules + $aRules;
  return $aRules;
}

// hook add_rewrite_rules function into rewrite_rules_array
add_filter('rewrite_rules_array', 'add_rewrite_rules');

# 找到模板目录下面的functions.php 添加下面代码
function add_query_vars($aVars) {
  $aVars[] = "idx";
  return $aVars;
}
// hook add_query_vars function into query_vars
add_filter('query_vars', 'add_query_vars');
 
function add_rewrite_rules($aRules) {
  $aNewRules = array('(health)/([^/]+)/?$' => 'index.php?category_name=$matches[1]&idx=$matches[2]');
  $aRules = $aNewRules + $aRules;
  return $aRules;
}
 
// hook add_rewrite_rules function into rewrite_rules_array
add_filter('rewrite_rules_array', 'add_rewrite_rules');

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

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

在需要获取idx参数值的寂寞中,如下代码就可以得到了:
#打印所有参数
var_dump($wp_query->query_vars);
#取得idx参数和值
$idx = get_query_var('idx');

#打印所有参数
var_dump($wp_query->query_vars);
#取得idx参数和值
$idx = get_query_var('idx');

热门栏目