双语版织梦 prenext

99ANYc3cd6
预计阅读时长 26 分钟
位置: 首页 织梦建站 正文

Part 1: 概念解释

中文版

“Prenext” 是什么?

双语版织梦 prenext
(图片来源网络,侵删)

“Prenext” 是一个由织梦CMS(DedeCMS)社区创造的词,是 “Previous” (上一篇)“Next” (下一篇) 的组合词,在网站文章内容页(article_article.htm 模板文件中),它通常显示为:

上一篇: [文章标题 A]
下一篇: [文章标题 B]

它的作用:

  1. 提升用户体验:方便用户在阅读完一篇文章后,无缝地继续浏览相关内容,增加用户在网站的停留时间。
  2. SEO优化:通过内部链接,将权重传递给网站的其他页面,有助于搜索引擎更好地抓取和索引网站的所有内容。
  3. 降低跳出率:为读者提供了明确的下一步行动指引,有效减少了读者看完一篇文章就离开网站的情况。

English Version

What is "Prenext"?

双语版织梦 prenext
(图片来源网络,侵删)

"Prenext" is a term popular within the DedeCMS community, a portmanteau of "Previous" and "Next". On an article detail page (in the article_article.htm template file), it is typically displayed as:

Previous Article: [Article Title A]
Next Article: [Article Title B]

Its Purpose:

  1. Enhance User Experience: It allows users to seamlessly navigate to related content after finishing an article, increasing their time on site.
  2. SEO Optimization: It passes link equity to other pages on the website through internal links, which helps search engines better crawl and index the site's content.
  3. Reduce Bounce Rate: It provides users with a clear call-to-action for what to do next, effectively preventing them from leaving the site immediately after reading a single article.

Part 2: 织梦核心代码

在织梦模板中,实现这个功能的核心代码非常简单,只需要一个标签。

双语版织梦 prenext
(图片来源网络,侵删)

中文版

模板标签:

{dede:prenext get='pre'/} {dede:prenext get='next'/}

参数说明:

  • get='pre':获取并显示 上一篇 文章的链接和标题。
  • get='next':获取并显示 下一篇 文章的链接和标题。

默认输出格式:

这个标签默认的输出格式是: <a href='文章链接'>文章标题</a>

使用示例:

article_article.htm 文件中,你可以这样使用:

<div class="prenext">
    <div class="prenext-box">
        <strong>上一篇:</strong>
        {dede:prenext get='pre'/}
    </div>
    <div class="prenext-box">
        <strong>下一篇:</strong>
        {dede:prenext get='next'/}
    </div>
</div>

English Version

Template Tag:

{dede:prenext get='pre'/} {dede:prenext get='next'/}

Parameter Explanation:

  • get='pre': Fetches and displays the link and title of the Previous article.
  • get='next': Fetches and displays the link and title of the Next article.

Default Output Format:

By default, this tag outputs: <a href='article_url'>article_title</a>

Usage Example:

In your article_article.htm file, you can use it like this:

<div class="prenext">
    <div class="prenext-box">
        <strong>Previous Article:</strong>
        {dede:prenext get='pre'/}
    </div>
    <div class="prenext-box">
        <strong>Next Article:</strong>
        {dede:prenext get='next'/}
    </div>
</div>

Part 3: 双语版实现方案

要让上一篇/下一篇链接也支持双语,我们不能仅仅依赖 {dede:prenext} 这个简单的标签,因为它只调用当前栏目的文章,我们需要结合SQL查询和自定义函数来实现跨栏目的调用。

这个方案将实现:

  • 根据当前语言环境(通过URL参数 ?lang=en 判断),调用对应语言分类下的上一篇/下一篇。
  • 提供完整的、可自定义的HTML输出。

Step 1: 准备工作 (Prerequisites)

  1. 栏目结构:确保你的网站栏目结构支持双语,创建一个顶级栏目“新闻 (News)”,其下有两个子栏目:“中文新闻 (Chinese News)” 和 “English News”。

  2. URL识别:你的网站需要有能力识别当前语言,最简单的方式是通过URL参数,

    • https://yourdomain.com/news/1.html (中文)
    • https://yourdomain.com/news/1.html?lang=en (英文)
    • 或者更优雅的方式是:https://yourdomain.com/en/news/1.html

    在本示例中,我们假设通过 $_GET['lang'] 来判断语言。

Step 2: 创建自定义函数

在你的织梦模板文件(head.htmfooter.htm)中加入以下PHP函数,织梦模板支持在 {dede:php}...{/dede:php} 标签内写PHP代码。

{dede:php}
function getBilingualPrenext($aid, $get = 'pre', $lang = 'cn')
{
    global $dsql;
    // 1. 获取当前文章的栏目ID
    $query = "SELECT typeid FROM `#@__archives` WHERE id = $aid";
    $row = $dsql->GetOne($query);
    $currentTypeId = $row['typeid'];
    // 2. 根据语言确定要查询的目标栏目ID
    // 假设中文栏目ID为1,英文栏目ID为2
    $targetTypeId = ($lang == 'en') ? 2 : 1;
    // 3. 构建SQL查询
    // 如果当前栏目就是目标栏目,则正常查询
    // 否则,在目标栏目下查找ID相近的文章
    $addSql = "";
    if ($currentTypeId != $targetTypeId) {
        // 在目标栏目下查找与当前文章ID最接近的文章
        // 这是一个简化的逻辑,实际可能需要更复杂的匹配
        if ($get == 'pre') {
            $addSql = " AND id < $aid ORDER BY id DESC";
        } else {
            $addSql = " AND id > $aid ORDER BY id ASC";
        }
    } else {
        // 如果在同一个栏目内,按常规的上一篇/下一篇逻辑
        if ($get == 'pre') {
            $addSql = " AND id < $aid ORDER BY id DESC";
        } else {
            $addSql = " AND id > $aid ORDER BY id ASC";
        }
    }
    $query = "SELECT id, title FROM `#@__archives` 
              WHERE typeid = $targetTypeId {$addSql} LIMIT 1";
    $row = $dsql->GetOne($query);
    if ($row) {
        $link = GetOneArchive($row['id']);
        return "<a href='{$link['arcurl']}'>{$row['title']}</a>";
    } else {
        // 如果找不到,返回提示文本
        $tipText = ($get == 'pre') ? '这是第一篇文章' : '没有下一篇文章了';
        $tipTextEn = ($get == 'pre') ? 'This is the first article' : 'No more articles';
        return $lang == 'en' ? $tipTextEn : $tipText;
    }
}
{/dede:php}

代码逻辑解释:

  1. 获取当前文章的栏目ID (typeid)。
  2. 根据 lang 参数('cn' 或 'en')确定目标栏目的ID(1代表中文,2代表英文)。
  3. 构建SQL查询,如果当前文章所在的栏目和目标语言栏目不同,它会在目标语言栏目下查找ID比当前文章大或小的文章。
  4. 如果找到文章,就生成一个标准的 <a> 标签链接。
  5. 如果找不到(已经是第一篇或最后一篇),则返回一个提示文本。

Step 3: 在模板中调用函数

你可以在你的 article_article.htm 模板中使用这个自定义函数了。

<!-- 双语上一篇/下一篇 -->
<div class="prenext">
    <div class="prenext-box">
        <strong>上一篇:</strong> / <strong>Previous Article:</strong>
        {dede:php}
        // 获取当前文章ID
        $currentAid = $aid;
        // 获取语言参数,如果不存在则默认为'cn'
        $currentLang = isset($_GET['lang']) && $_GET['lang'] == 'en' ? 'en' : 'cn';
        echo getBilingualPrenext($currentAid, 'pre', $currentLang);
        {/dede:php}
    </div>
    <div class="prenext-box">
        <strong>下一篇:</strong> / <strong>Next Article:</strong>
        {dede:php}
        // 获取当前文章ID
        $currentAid = $aid;
        // 获取语言参数
        $currentLang = isset($_GET['lang']) && $_GET['lang'] == 'en' ? 'en' : 'cn';
        echo getBilingualPrenext($currentAid, 'next', $currentLang);
        {/dede:php}
    </div>
</div>

使用说明:

  • 将上面的代码替换掉你模板中原来的 {dede:prenext}
  • 访问文章时,通过URL参数 ?lang=en 即可看到英文版的上一篇/下一篇链接。
  • 访问不带 ?lang=en 的URL,则会看到中文版的链接。

进阶优化: 你可以进一步美化输出,例如为找不到文章的情况添加一个 <span class="disabled"> 标签,以便用CSS来控制样式。

// 在函数的 else 部分
return "<span class='disabled'>" . ($lang == 'en' ? $tipTextEn : $tipText) . "</span>";

然后在CSS中定义:

.prenext a.disabled {
    color: #999;
    cursor: not-allowed;
    text-decoration: none;
}

这样,你的织梦网站就拥有了一个功能完善、支持双语的上一篇/下一篇功能了。

-- 展开阅读全文 --
头像
织梦dedecms栏目标签
« 上一篇 2025-12-31
dede首页如何调用图片集所有图片?
下一篇 » 2025-12-31

相关文章

取消
微信二维码
支付宝二维码