DEDEBIZ内容页分页标题的修改

在DEDEBIZ默认模板里,文章内容页是带有分页目录效果的。如下图:

这是利用文章分页实现的。在编辑文章时,在需要分页的地方插入分页符#p#分页标题#e#即可。但默认的分页标题不能获取本篇文章的标题。所以本文介绍的就是把固定的分页标题修改为文章的标题。

打开/system/archive/archives.class.php,搜索:获得动态文档分页标题, 大约在858行,

 

把从865行到928行的所有代码替换为:

function GetPageTitlesDM($styleName, $PageNo)
{
    global $cfg_rewrite, $cfg_cmsurl;
    
    // 检查分页必要性
    if ($this->TotalPage <= 1) {
        return "";
    }

    // 安全获取文章ID和标题
    $aid = isset($this->ArcID) ? (int)$this->ArcID : 0;
    $title = isset($this->Fields['title']) ? trim($this->Fields['title']) : '分页标题';
    $title = preg_replace('/-第\d+页$/', '', $title);

    // 从URL解析文章ID(增强兼容性)
    if ($aid == 0 && isset($_SERVER['REQUEST_URI'])) {
        if (preg_match('/\/(\d+)(?:-(\d+))?\.html$/', $_SERVER['REQUEST_URI'], $matches)) {
            $aid = (int)$matches[1];
        }
    }
    if ($aid == 0) return "";

    // 生成下拉菜单
    $revalue = "<select class='form-control w-25' onchange='location.href=this.options[this.selectedIndex].value'>";
    
    for ($i = 1; $i <= $this->TotalPage; $i++) {
        // 生成URL - 与分页代码保持一致
        if ($cfg_rewrite == 'Y') {
            $pageUrl = ($i == 1) 
                ? "{$cfg_cmsurl}/article/{$aid}.html" 
                : "{$cfg_cmsurl}/article/{$aid}-{$i}.html";
        } else {
            $pageUrl = isset($this->Fields['phpurl']) 
                ? "{$this->Fields['phpurl']}/view.php?aid={$aid}&PageNo={$i}" 
                : "#";
        }

        // 生成选项文本
        $optionText = htmlspecialchars($title)." - 第{$i}页";
        $selected = ($PageNo == $i) ? ' selected' : '';
        
        $revalue .= "<option value='".htmlspecialchars($pageUrl)."'{$selected}>{$optionText}</option>";
    }
    
    $revalue .= "</select>";
    return $revalue;
}

保存。刷新页面。我们看到的就是下面的效果:

上面是在开启伪静态的情况的修改的,用的是默认的伪静态规则,文章的目录为固定为article,规则为:

rewrite ^/article/([0-9]+)-([0-9]+).html$ /apps/view.php?aid=$1&PageNo=$2;

如果你的伪静态是下面的这种DEDEBIZ伪静态URL地址和生成静态URL地址保持一致的方法

那就得用下面代码替换:

function GetPageTitlesDM($styleName, $PageNo)
{
    global $cfg_rewrite;
    
    // 检查分页必要性
    if ($this->TotalPage <= 1) {
        return "";
    }

    // 安全获取文章ID和标题
    $aid = isset($this->ArcID) ? (int)$this->ArcID : 0;
    $title = isset($this->Fields['title']) ? trim($this->Fields['title']) : '分页标题';
    $title = preg_replace('/-第\d+页$/', '', $title);

    // 从URL解析文章ID(增强兼容性)
    if ($aid == 0 && isset($_SERVER['REQUEST_URI'])) {
        if (preg_match('/\/(\d+)(?:-(\d+))?\.html$/', $_SERVER['REQUEST_URI'], $matches)) {
            $aid = (int)$matches[1];
        }
    }
    if ($aid == 0) return "";

    // 安全获取栏目目录
    $typedir = '';
    if (isset($this->TypeLink->TypeInfos['typedir'])) {
        $typedir = str_replace(['{cmspath}/', '{typedir}/'], '', $this->TypeLink->TypeInfos['typedir']);
        $typedir = trim($typedir, '/');
    }
    if (empty($typedir)) {
        $typedir = 'article';
    }

    // 生成下拉菜单
    $revalue = "<select class='form-control w-25' onchange='location.href=this.options[this.selectedIndex].value'>";
    
    for ($i = 1; $i <= $this->TotalPage; $i++) {
        // 生成URL
        $pageUrl = ($i == 1) 
            ? "/{$typedir}/{$aid}.html" 
            : "/{$typedir}/{$aid}-{$i}.html";
        
        // 兼容非伪静态
        if (isset($cfg_rewrite) && $cfg_rewrite != 'Y' && isset($this->Fields['phpurl'])) {
            $pageUrl = $this->Fields['phpurl']."/view.php?aid={$aid}&PageNo={$i}";
        }

        // 生成选项文本
        $optionText = htmlspecialchars($title)." - 第{$i}页";
        $selected = ($PageNo == $i) ? ' selected' : '';
        
        $revalue .= "<option value='".htmlspecialchars($pageUrl)."'{$selected}>{$optionText}</option>";
    }
    
    $revalue .= "</select>";
    return $revalue;
}

如果你的所有文章没有分页,就不用修改任何代码。

END

THE END