You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为不同PHP页面单独设置自定义HTML title?

解决PHP页面自定义HTML Title不生效的问题

问题原因

你提前设置$g5_head_title后再引入head.sub.php,但这个文件里的逻辑会重新给$g5_head_title赋值,直接覆盖你之前设置的内容,所以自定义标题没生效。

解决方案

分两种场景处理,满足你部分页面保留默认、部分自定义的需求:

场景1:沿用「自定义标题 | 站点名称」格式

如果想保持现有标题的拼接规则,直接设置$g5['title']就行,现有逻辑会自动帮你加上站点后缀:

<?
    $g5['title'] = 'New Title'; // 只填自定义标题部分
    include_once("head.sub.php");
?>

引入后会自动生成类似New Title | example.com的标题(example.com是$config['cf_title']的值)。

场景2:完全自定义标题内容

如果需要完全自由设置标题(比如不需要加站点后缀),得修改head.sub.php里的逻辑,先判断$g5_head_title有没有被提前设置,避免覆盖:
修改head.sub.php中的title逻辑代码:

// 优先使用已设置的自定义标题,没有再走默认逻辑
if (!isset($g5_head_title)) {
    if (!isset($g5['title'])) {
        $g5['title'] = $config['cf_title'];
        $g5_head_title = $g5['title'];
    } else {
        $g5_head_title = $g5['title'];
        $g5_head_title .= " | ".$config['cf_title'];
    }
}

之后在需要自定义标题的页面,直接设置$g5_head_title即可:

<?
    $g5_head_title = 'New Title | example.com'; // 直接写完整标题
    include_once("head.sub.php");
?>

不需要自定义的页面保持原有代码,会自动使用默认标题逻辑。

内容的提问来源于stack exchange,提问作者user19906832

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 09:00:25