删除 WordPress 上的作者前缀 [英] Remove author prefix on WordPress

查看:29
本文介绍了删除 WordPress 上的作者前缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何删除 WordPress 网站上的作者前缀,我已经快速搜索了谷歌,但只找到了我不想诉诸的 htaccess 重定向.

How can I remove the author prefix on a WordPress website, I have done a quick Google but have only found htaccess redirects which I don't want to resort to.

为了澄清我想转这个:

http://www.domain.com/author/cameron/

进入这个

http://www.domain.com/cameron/

我不想使用任何类型的重定向,而是我可以在 functions.php 文件中使用的实际 PHP 代码,因为我希望网站上所有使用作者内容的链接自动更新而不保留原始内容链接,然后重定向到新链接.

I don't want to use any redirects of any kind, but actual PHP code I can use in the functions.php file, as I want all links across the site that use the author stuff to auto update without keeping there original links and then redirecting to the new one.

谢谢

推荐答案

您基本上需要添加 WP 重写规则,以便以所需的形式匹配每个用户的姓名.这就是 WP No Category Base 对类别的作用,所以大多数我的答案中的大部分代码改编自该插件.

You basically need to add WP rewrite rules to match the names of each of your users in the desired form. This is what the WP No Category Base does for categories, so most of the code in my answer is adapted from that plugin.

插件的主要部分是一个函数,它与author_rewrite_rules 过滤器挂钩并替换作者重写规则.这将检索所有用户名并专门为每个用户添加重写规则(以下不会处理提要,因此如果需要,请查看 WP No Category Base 源).

The primary part of the plugin is a function which hooks into the author_rewrite_rules filter and replaces the author rewrite rules. This retrieves all the user names and adds a rewrite rule specifically for each user (the below won't handle feeds, so look at the WP No Category Base source if you need that).

add_filter('author_rewrite_rules', 'no_author_base_rewrite_rules');
function no_author_base_rewrite_rules($author_rewrite) { 
    global $wpdb;
    $author_rewrite = array();
    $authors = $wpdb->get_results("SELECT user_nicename AS nicename from $wpdb->users");    
    foreach($authors as $author) {
        $author_rewrite["({$author->nicename})/page/?([0-9]+)/?$"] = 'index.php?author_name=$matches[1]&paged=$matches[2]';
        $author_rewrite["({$author->nicename})/?$"] = 'index.php?author_name=$matches[1]';
    }   
    return $author_rewrite;
}

插件的另一个关键部分是一个函数,它与 author_link 过滤器挂钩,并从返回的 URL 中删除作者"基础.

The other key part of the plugin is a function which hooks into the author_link filter and removes the 'author' base from the returned URL.

add_filter('author_link', 'no_author_base', 1000, 2);
function no_author_base($link, $author_id) {
    $link_base = trailingslashit(get_option('home'));
    $link = preg_replace("|^{$link_base}author/|", '', $link);
    return $link_base . $link;
}

请参阅此要点:http://gist.github.com/564465

这不会处理来自旧样式作者 URL 的重定向,如果需要,请再次查看 WP No Category Base 源.

This doesn't handle redirection from the old style author URLs, again, see the WP No Category Base source if you need to do that.

这篇关于删除 WordPress 上的作者前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆