目录
在开发 WordPress 社区类或问答类网站时,我们往往需要为每个用户提供一个“用户中心”,同时也需要为作者页面扩展自定义的子页面,比如“他的关注”、“他的粉丝”等。这篇文章将介绍如何在 WordPress 主题中优雅地添加自定义端点,实现这两个功能,并支持分页。
/usercenter/edit/
、/usercenter/collect/
等用户中心页面/author/john/collect/
、/author/john/followed/page/2/
等作者子页面/usercenter/questions/page/3/
在 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' );
让 WordPress 能识别我们新增的 usercenter
和 author_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,
],
// 可按需继续添加
];
}
我们写一个函数,既支持当前用户中心,也支持指定作者页面端点:
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>
⚠️ 别忘了这一步!否则你的规则不会生效。
后台进入「设置 → 固定链接」页面,点击一次“保存”按钮即可刷新。
function wpshequ_theme_activation() {
wpshequ_add_rewrite_rules(); // 保证规则在刷新前已注册
flush_rewrite_rules();
}
add_action('after_switch_theme', 'wpshequ_theme_activation');
通过上面的方式,你可以在 WordPress 主题中优雅地实现:
这种结构对于构建社区类、问答类、甚至电商类用户中心都是极好的基础。
WordPress日记主要承接WordPress主题定制开发、PSD转WordPress、WordPress仿站以及以WordPress为管理后端的小程序、APP,我们一直秉持“做一个项目,交一个朋友”的理念,希望您是我们下一个朋友。如果您有WordPress主题开发需求,可随时联系QQ:919985494 微信:18539976310
还没有任何评论,你来说两句吧