12年专注,只做WordPress定制开发一件事

搜索
登录/注册

WordPress 主题开发中自定义用户中心和作者存档页链接结构的方法

王超
2025-05-09
WordPress主题开发
13 次

在开发 WordPress 社区类或问答类网站时,我们往往需要为每个用户提供一个“用户中心”,同时也需要为作者页面扩展自定义的子页面,比如“他的关注”、“他的粉丝”等。这篇文章将介绍如何在 WordPress 主题中优雅地添加自定义端点,实现这两个功能,并支持分页。


一、实现目标

  • /usercenter/edit//usercenter/collect/ 等用户中心页面
  • /author/john/collect//author/john/followed/page/2/ 等作者子页面
  • 支持分页参数:如 /usercenter/questions/page/3/
  • 每个端点映射到主题内特定的模板文件
  • 所有逻辑在 主题 functions.php 或主题包含文件中实现

二、添加自定义 Rewrite 规则

functions.php 中添加以下代码:

// 添加 rewrite 规则
function wpshequ_add_rewrite_rules() {
    // 用户中心规则
    add_rewrite_rule( '^usercenter/([a-z0-9-]+)/page/([0-9]{1,})/?$', 'index.php?usercenter=$matches[1]&paged=$matches[2]', 'top' );
    add_rewrite_rule( '^usercenter/([a-z0-9-]+)[/]?$', 'index.php?usercenter=$matches[1]', 'top' );

    // 作者存档扩展规则
    add_rewrite_rule( '^author/([a-z0-9-]+)/([a-z0-9-]+)/page/([0-9]{1,})/?$', 'index.php?author_name=$matches[1]&author_center=$matches[2]&paged=$matches[3]', 'top' );
    add_rewrite_rule( '^author/([a-z0-9-]+)/([a-z0-9-]+)/?$', 'index.php?author_name=$matches[1]&author_center=$matches[2]', 'top' );
}
add_action( 'init', 'wpshequ_add_rewrite_rules' );

三、注册自定义 Query Vars

让 WordPress 能识别我们新增的 usercenterauthor_center 参数:

function wpshequ_usercenter_query_vars($vars) {
    $vars[] = 'usercenter';
    $vars[] = 'author_center';
    $vars[] = 'paged';
    return $vars;
}
add_filter('query_vars', 'wpshequ_usercenter_query_vars');

四、路由对应模板文件

我们可以根据是否存在 usercenter 参数来返回我们自己的模板文件,比如 page-usercenter.php

add_filter( 'template_include', function( $template ) {
    if ( get_query_var( 'usercenter' ) !== '' ) {
        return get_template_directory() . '/page-usercenter.php';
    }
    return $template;
} );

作者端点页如果需要使用不同模板,也可以再写一个类似判断 author_center 的模板判断。


五、定义所有端点类型(配置化)

为了便于扩展和使用,可以将所有用户中心和作者中心支持的“子页”通过配置数组统一管理:

function wpshequ_usercenter_get_endpoints() {
    return [
        'edit' => [
            'icon' => 'dashicons-admin-users',
            'label' => '个人资料',
            'author_page_label' => '',
            'author_page_show' => false,
            'show_in_menu' => true,
        ],
        'collect' => [
            'icon' => 'dashicons-star-filled',
            'label' => '我的收藏',
            'author_page_label' => '他的收藏',
            'author_page_show' => true,
            'show_in_menu' => true,
        ],
        'followed' => [
            'icon' => 'dashicons-list-view',
            'label' => '我的关注',
            'author_page_label' => '他的关注',
            'author_page_show' => true,
            'show_in_menu' => true,
        ],
        'questions' => [
            'icon' => 'dashicons-format-status',
            'label' => '我的话题',
            'author_page_label' => '他的话题',
            'author_page_show' => true,
            'show_in_menu' => true,
        ],
        'anwsers' => [
            'icon' => 'dashicons-admin-comments',
            'label' => '我的回答',
            'author_page_label' => '他的回答',
            'author_page_show' => true,
            'show_in_menu' => true,
        ],
        'points-log' => [
            'icon' => 'dashicons-awards',
            'label' => '声望记录',
            'author_page_label' => '声望记录',
            'author_page_show' => true,
            'show_in_menu' => true,
        ],
        // 可按需继续添加
    ];
}

六、生成端点 URL 的方法(用户/作者通用)

我们写一个函数,既支持当前用户中心,也支持指定作者页面端点:

function wpshequ_get_usercenter_endpoint_url($endpoint, $author_id = null) {
    if ($author_id) {
        return esc_url(get_author_posts_url($author_id) . $endpoint . '/');
    } else {
        return esc_url(home_url("usercenter/{$endpoint}/"));
    }
}

使用例子:

<a href="<?php echo wpshequ_get_usercenter_endpoint_url('collect'); ?>">我的收藏</a>
<a href="<?php echo wpshequ_get_usercenter_endpoint_url('collect', 123); ?>">他的收藏</a>

七、记得刷新 Rewrite 规则!

⚠️ 别忘了这一步!否则你的规则不会生效。

方法一:手动方式(开发时)

后台进入「设置 → 固定链接」页面,点击一次“保存”按钮即可刷新。

方法二:自动刷新(建议主题首次启用时加上)

function wpshequ_theme_activation() {
    wpshequ_add_rewrite_rules(); // 保证规则在刷新前已注册
    flush_rewrite_rules();
}
add_action('after_switch_theme', 'wpshequ_theme_activation');

✅ 总结

通过上面的方式,你可以在 WordPress 主题中优雅地实现:

  • 用户中心和作者子页面的端点规则
  • 支持分页
  • 自动模板分发
  • 支持前端导航菜单自动生成端点链接

这种结构对于构建社区类、问答类、甚至电商类用户中心都是极好的基础。

WordPress主题开发业务联系方式

WordPress日记主要承接WordPress主题定制开发PSD转WordPressWordPress仿站以及以WordPress为管理后端的小程序、APP,我们一直秉持“做一个项目,交一个朋友”的理念,希望您是我们下一个朋友。如果您有WordPress主题开发需求,可随时联系QQ:919985494 微信:18539976310

0 条评论

无意义的评论将很快被删除,账号将被禁止发言。 0/500
发表评论
 
  1. 还没有任何评论,你来说两句吧

搜索

在线客服
嘿,有问题找我来帮您!