目标:在导航栏「联系管理组」之后新增一个「幸运大转盘」按钮,指向 /game/index.php,
要求点击后在新标签页打开,并且进入该页面时按钮正确高亮。
一、基本信息
| 项目 | 内容 |
| 适用版本 | NexusPHP v1.10.3(xiaomlove/nexusphp,PHP 8 分支) |
| 修改文件 | include/functions.php(即你上传的 fn_v1.10.3.php,共 6663 行) |
| 修改函数 | menu()(函数定义在第 2322 行) |
| 修改函数 | stdhead()内第 2647 行 menu(); |
| 改动数量 | 共 2 处,合计新增 3 行代码 |
| 是否需改别的文件 | 不需要(单站中文显示;如需多语言见文末附录) |
说明:导航栏不是 HTML 模板,而是由 menu() 动态 print 出来的;$selected 变量负责判断当前页并给出高亮。
你的导航项顺序(首页→论坛→种子→候选→求种→发布→字幕→排行榜→日志→规则→常见问题→管理组→联系管理组)与该函数完全一致,可确认目标就在此函数内。
二、改动总览
| 序号 | 定位 | 操作 | 作用 |
| 改动 1 | 第 2329 行之后、第 2330 行之前 | 插入 2 行 | 判断链最前面加 /game/分支,使访问 /game/index.php时 $selected = "game" |
| 改动 2 | 第 2397 行之后、第 2398 行之前 | 插入 1 行 | 在「联系管理组」右侧输出「幸运大转盘」链接,并支持高亮、新标签打开 |
注意:改前务必先备份 include/functions.php。语法写错会导致全站白屏。
三、改动 1 详解(判断链,决定高亮)
3.1 原始代码(第 2329–2331 行)
$script_name = $_SERVER["SCRIPT_NAME"];
if (preg_match("/index/i", $script_name)) {
$selected = "home";3.2 修改后代码(第 2329–2332 行)
$script_name = $_SERVER["SCRIPT_NAME"];
if (preg_match("/game/i", $script_name)) { // ★新增:必须放在 index 判断之前
$selected = "game"; // ★新增
}elseif (preg_match("/index/i", $script_name)) { // 原来的 if 改为 elseif,整体顺延一行
$selected = "home";3.3 为什么必须放在 /index/i 之前
第 2330 行的 preg_match("/index/i", ...) 是子串匹配,而你的地址 /game/index.php 含有 index 这 5 个字母。
一旦 /index/i 先命中,$selected 会被设为 "home",后面整条 elseif 链全部跳过,game 分支成为死代码,结果就是「大转盘页面高亮成首页」。
把 /game/i 提到最前面,访问 /game/index.php 时第一个就命中,$selected = "game",后面的规则不再执行,高亮正确。
本项目全站没有其它含 game 的路径,不会误判。
四、改动 2 详解(输出菜单按钮)
4.1 原始代码(第 2397–2398 行)
print ("<li" . ($selected == "contactstaff" ? " class=\"selected\"" : "") . "><a href=\"contactstaff.php\">".$lang_functions['text_contactstaff']."</a></li>");
print ("</ul>");4.2 修改后代码(第 2399–2402 行)
print ("<li" . ($selected == "contactstaff" ? " class=\"selected\"" : "") . "><a href=\"contactstaff.php\">".$lang_functions['text_contactstaff']."</a></li>");
print ("<li" . ($selected == "game" ? " class=\"selected\"" : "") . "><a href=\"/game/index.php\" target=\"_blank\">幸运大转盘</a></li>"); // ★新增
print ("</ul>");4.3 逐段解读
| 片段 | 含义 |
| <li | 菜单项(列表项)开始 |
| ($selected == "game" ? " class=\"selected\"" : "") | 当前页是 game 时输出高亮样式,否则输出空串 |
| <a href=\"/game/index.php\" | 链接地址,\/是被 PHP 双引号字符串转义的斜杠(等价于 /) |
| target=\"_blank\" | 在新标签页打开(本项目的既有写法,参见同函数第 2377 行外部论坛链接) |
| 幸运大转盘 | 按钮显示文字 |
| </a></li> | 关闭链接与列表项 |
转义要点:整段是 PHP 双引号字符串,因此 HTML 属性里的双引号必须写成 \",不能写裸 ",否则会提前截断字符串、直接语法报错白屏。这与原代码 class=\"selected\" 的写法一致。
五、改动后完整代码(第 2329 ~ 2401 行,含注释)
可直接整段替换核对。// ★ 为本次新增内容及注释。
$script_name = $_SERVER["SCRIPT_NAME"];
// ★【改动1】把 game 判断提到最前面,避免被下方宽松的 /index/i 子串匹配误吃
if (preg_match("/game/i", $script_name)) {
$selected = "game"; // 当前页为大转盘时,标记为 game
}elseif (preg_match("/index/i", $script_name)) { // 原第 2330 行,由 if 改为 elseif
$selected = "home";
}elseif (preg_match("/forums/i", $script_name)) {
$selected = "forums";
}elseif (preg_match("/torrents/i", $script_name)) {
$selected = "torrents";
}elseif (preg_match("/special/i", $script_name)) {
$selected = "special";
}elseif (preg_match("/offers/i", $script_name) OR preg_match("/offcomment/i", $script_name)) {
$selected = "offers";
}elseif (preg_match("/requests/i", $script_name)) {
$selected = "requests";
}elseif (preg_match("/upload/i", $script_name)) {
$selected = "upload";
}elseif (preg_match("/subtitles/i", $script_name)) {
$selected = "subtitles";
}elseif (preg_match("/usercp/i", $script_name)) {
$selected = "usercp";
}elseif (preg_match("/topten/i", $script_name)) {
$selected = "topten";
}elseif (preg_match("/log/i", $script_name)) {
$selected = "log";
}elseif (preg_match("/rules/i", $script_name)) {
$selected = "rules";
}elseif (preg_match("/faq/i", $script_name)) {
$selected = "faq";
}elseif (preg_match("/contactstaff/i", $script_name)) {
$selected = "contactstaff";
}elseif (preg_match("/staff/i", $script_name)) {
$selected = "staff";
}else
$selected = "";
$menu = apply_filter('nexus_menu'); // 插件钩子:若返回非空,将整段替换默认菜单
print ("<div id=\"nav\">");
if ($menu) {
print $menu;
} else {
$lang = get_langfolder_cookie();
$normalSectionName = get_searchbox_value(get_setting('main.browsecat'), 'section_name');
$specialSectionName = get_searchbox_value(get_setting('main.specialcat'), 'section_name');
print ("<ul id=\"mainmenu\" class=\"menu\">");
print ("<li" . ($selected == "home" ? " class=\"selected\"" : "") . "><a href=\"index.php\">" . $lang_functions['text_home'] . "</a></li>");
if ($enableextforum != 'yes')
print ("<li" . ($selected == "forums" ? " class=\"selected\"" : "") . "><a href=\"forums.php\">".$lang_functions['text_forums']."</a></li>");
else
print ("<li" . ($selected == "forums" ? " class=\"selected\"" : "") . "><a href=\"" . $extforumurl."\" target=\"_blank\">".$lang_functions['text_forums']."</a></li>");
print ("<li" . ($selected == "torrents" ? " class=\"selected\"" : "") . "><a href=\"torrents.php\" rel='sub-menu'>".($normalSectionName[$lang] ?? $lang_functions['text_torrents'])."</a></li>");
if ($enablespecial == 'yes' && user_can('view_special_torrent'))
print ("<li" . ($selected == "special" ? " class=\"selected\"" : "") . "><a href=\"special.php\">".($specialSectionName[$lang] ?? $lang_functions['text_special'])."</a></li>");
if ($enableoffer == 'yes')
print ("<li" . ($selected == "offers" ? " class=\"selected\"" : "") . "><a href=\"offers.php\">".$lang_functions['text_offers']."</a></li>");
if ($enablerequest == 'yes')
print ("<li" . ($selected == "requests" ? " class=\"selected\"" : "") . "><a href=\"viewrequests.php\">".$lang_functions['text_request']."</a></li>");
print ("<li" . ($selected == "upload" ? " class=\"selected\"" : "") . "><a href=\"upload.php\">".$lang_functions['text_upload']."</a></li>");
print ("<li" . ($selected == "subtitles" ? " class=\"selected\"" : "") . "><a href=\"subtitles.php\">".$lang_functions['text_subtitles']."</a></li>");
// print ("<li" . ($selected == "usercp" ? " class=\"selected\"" : "") . "><a href=\"usercp.php\">".$lang_functions['text_user_cp']."</a></li>");
if (user_can('topten')) {
print ("<li" . ($selected == "topten" ? " class=\"selected\"" : "") . "><a href=\"topten.php\">".$lang_functions['text_top_ten']."</a></li>");
}
if (user_can('log')) {
print ("<li" . ($selected == "log" ? " class=\"selected\"" : "") . "><a href=\"log.php\">".$lang_functions['text_log']."</a></li>");
}
print ("<li" . ($selected == "rules" ? " class=\"selected\"" : "") . "><a href=\"rules.php\">".$lang_functions['text_rules']."</a></li>");
print ("<li" . ($selected == "faq" ? " class=\"selected\"" : "") . "><a href=\"faq.php\">".$lang_functions['text_faq']."</a></li>");
if (user_can('staffmem')) {
print ("<li" . ($selected == "staff" ? " class=\"selected\"" : "") . "><a href=\"staff.php\">".$lang_functions['text_staff']."</a></li>");
}
print ("<li" . ($selected == "contactstaff" ? " class=\"selected\"" : "") . "><a href=\"contactstaff.php\">".$lang_functions['text_contactstaff']."</a></li>");
// ★【改动2】在「联系管理组」之后新增「幸运大转盘」;target="_blank" 表示新标签页打开
print ("<li" . ($selected == "game" ? " class=\"selected\"" : "") . "><a href=\"/game/index.php\" target=\"_blank\">幸运大转盘</a></li>");
print ("</ul>");
}
print ("</div>");六、注意事项
1. 务必备份:改前复制一份 include/functions.php,出问题可立即还原。
2. nexus_menu 钩子(第 2364 行):$menu = apply_filter('nexus_menu'); 的逻辑是「只要 $menu 非空,就整段替换默认菜单」。若你的大转盘插件自己挂了该钩子,本次在 else 分支里的改动不会显示,此时应改插件而非此处。未装此类插件时,本改动即为正解。
3. 链接绝对路径:href="/game/index.php" 以站点根目录为基准。
l 站点装在根目录(如 http://域名/)→ 正确。
l 站点装在子目录(如 http://域名/pt/)→ 会跳到 http://域名/game/index.php,导致 404。此时请改用相对写法:href=\"game/index.php\"(与其它菜单项风格一致)。
4. target 转义:target=\"_blank\" 中的双引号用 \" 转义,不要写成裸 target="_blank"。
5. 可选加固:如需防止新标签页反向操作原页(reverse tabnabbing),可再加 rel:target=\"_blank\" rel=\"noopener noreferrer\"。加不加都不影响正常新窗口打开。
6. 缓存:menu() 本身无缓存;若站点开了页面缓存,改完清一次缓存再验证。
七、验证与回滚
验证步骤:
1. 保存文件,刷新站点首页,导航栏最右侧应出现「幸运大转盘」。
2. 点击它:应在新标签页打开 /game/index.php,原页面保持不动。
3. 在该页面观察导航栏:「幸运大转盘」应带高亮样式(其它项不亮)。
4. 单击普通菜单项(如「论坛」)再回来,确认高亮能正确切换、无残留。
回滚: 用备份覆盖 include/functions.php 即可;或手动删除本次新增的 3 行、并把 }elseif (preg_match("/index/i"...) 还原为 if (preg_match("/index/i"...)。
附录:多语言站点可选做法
若站点开启了多语言,规范做法是把文字放进语言包,而不是写死中文:
1. 在 lang/chs/lang_functions.php 中(第 327 行 text_contactstaff 附近)新增:
'text_lucky_wheel' => '幸运大转盘',
2. 其它语言目录(lang/cht、lang/en 等)按需各加一份。
3. 把改动 2 的文字部分替换为变量:
print ("<li" . ($selected == "game" ? " class=\"selected\"" : "") . "><a href=\"/game/index.php\" target=\"_blank\">".$lang_functions['text_lucky_wheel']."</a></li>");
仅单站、仅显示中文时,用正文中的写死方式最省事,无需改动语言文件。
返回顶部