【问题标题】:Wordpress custom author urlsWordpress 自定义作者网址
【发布时间】:2013-05-04 21:27:06
【问题描述】:

我想实现自定义作者网址。

目前作者网址是这样的: http://site.com/author/author-name/

我想做类似的事情 http://site.com/my-custom-url-here

对于每个单独的用户。 我尝试使用author_rewrite_rules 过滤器,使用以下代码,这可以正确转换 url,但是当我浏览 url 时,这会给我一个找不到页面的消息

add_filter('author_link', 'no_author_base', 1000, 3);  
function no_author_base($link, $author_id) {  
    $link_base = trailingslashit(get_option('home'));  
    $link = preg_replace("|^{$link_base}author/|", '', $link);  
    return $link_base . $link;  
}  
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;  
}  

任何帮助将不胜感激!

谢谢。 更新

问题解决了!,基本上我不知道调用flush_rewrite_rules函数。

【问题讨论】:

标签: wordpress


【解决方案1】:

解决了!

我是这样做的:

我不需要 author_link 过滤器,所以我删除了它,我的自定义 url 存储在 usermeta 表中,所以我获取它们并将它们传递到重写数组,最重要的是 我不知道刷新重写缓存,这就是我的原始代码不起作用的原因

完整代码如下:

add_filter('author_rewrite_rules', 'my_author_url_with_custom_url_rewrite_rules');
function my_author_url_with_custom_url_rewrite_rules($author_rewrite) {

  global $wpdb;
  $author_rewrite = array();
  $authors = $wpdb->get_results("SELECT ID, user_nicename AS nicename, meta_value as profile_name
                                    from $wpdb->users 
                                    LEFT JOIN wp_usermeta ON wp_usermeta.user_id = $wpdb->users.ID
                                    WHERE  meta_key = 'profile_name'");    

  foreach ($authors as $author) {
    $author_rewrite["{$author->profile_name}/page/?([0-9]+)/?$"] = 'index.php?author_name=' . $author->nicename . '&paged=$matches[1]';
    $author_rewrite["{$author->profile_name}/?$"] = "index.php?author_name={$author->nicename}";
  }
  return $author_rewrite;
}
flush_rewrite_rules(false);

在完成重写规则后不要忘记评论flush_rewrite_rules调用,它非常昂贵!

【讨论】:

  • 这在 WP 3.8.1 中有效吗?即使在刷新永久链接后它对我也不起作用
【解决方案2】:

试试这个。获取您的作者网址。

<?php the_author_posts_link(); ?>

如果发布管理员或作者并希望在一页中显示他/她的所有帖子。因此,您需要自动获取他/她的网址。 那就试试这个

【讨论】:

  • 这与问题无关,即自定义作者网址。
猜你喜欢
  • 1970-01-01
  • 2011-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多