WordPress 作者永久链接

发布于 2024-10-18 04:32:41 字数 283 浏览 2 评论 0原文

我知道如何更改作者永久链接的基数,但是在我的网站上,我不是通过用户名而是通过基于用户 ID 的数字来引用用户,因此 5 号用户写了这篇文章,而不是 JohnDoe123 写了这篇文章。

当我转到用户档案时,问题就出现了,而不是看到类似 example.com/authors/5/ 的内容,而是看到 example.com/authors/johndoe123/ 。

如何更改永久链接以便可以使用以下结构提取作者档案? :

[wordpress_site_url]/authors/[user_ID]/

I know how to change the base of the author permalinks, however on my site, I refer to users not by their username but by a number based on their User ID, so User number 5 wrote this post, rather than JohnDoe123 wrote this post.

The problem comes when I go to that users archives and instead of seeing something like example.com/authors/5/ I see example.com/authors/johndoe123/ .

How do I change permalinks so that I can pull up author archives using the following structure? :

[wordpress_site_url]/authors/[user_ID]/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

聚集的泪 2024-10-25 04:32:41

这可以通过为每个用户添加新的重写规则来完成,其方式与更改或删除作者库时完全相同。因此,根据之前的答案改编代码,您可以添加重写规则如下:

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

然后过滤作者链接:

add_filter('author_link', 'my_author_url_with_id', 1000, 2);
function my_author_url_with_id($link, $author_id) {
  $link_base = trailingslashit(get_option('home'));
  $link = "authors/$author_id";
  return $link_base . $link;
}

实际上,我认为在这种情况下您不需要为每个用户创建规则,以下两个规则应该足够了:

add_filter('author_rewrite_rules', 'my_author_url_with_id_rewrite_rules');
function my_author_url_with_id_rewrite_rules($author_rewrite) {
  $author_rewrite = array();
  $author_rewrite["authors/([0-9]+)/page/?([0-9]+)/?$"] = 'index.php?author=$matches[1]&paged=$matches[2]';
  $author_rewrite["authors/([0-9]+)/?$"] = 'index.php?author=$matches[1]';
  return $author_rewrite;
}

This can be done by adding new rewrite rules for each user in exactly the same way you would when changing or removing the author base. So, adapting code from a previous answer, you would add your rewrite rules something like this:

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

And then filter the author link:

add_filter('author_link', 'my_author_url_with_id', 1000, 2);
function my_author_url_with_id($link, $author_id) {
  $link_base = trailingslashit(get_option('home'));
  $link = "authors/$author_id";
  return $link_base . $link;
}

Actually I don't think you need to create rules for each user in this case, the following two rules should suffice:

add_filter('author_rewrite_rules', 'my_author_url_with_id_rewrite_rules');
function my_author_url_with_id_rewrite_rules($author_rewrite) {
  $author_rewrite = array();
  $author_rewrite["authors/([0-9]+)/page/?([0-9]+)/?$"] = 'index.php?author=$matches[1]&paged=$matches[2]';
  $author_rewrite["authors/([0-9]+)/?$"] = 'index.php?author=$matches[1]';
  return $author_rewrite;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文