调用文章摘要(最常用、最推荐)
这是最标准、最高效的方法,在后台发布文章时,手动填写“(description字段),然后在首页模板中调用它,这样做的好处是:
- 速度快:摘要字段是专门为列表页和首页设计的,数据库查询效率高。
- SEO友好:摘要可以直接用作
<meta name="description">,有利于搜索引擎优化。 - 页面整洁:可以精确控制首页显示的内容长度,避免页面过长。
操作步骤:
-
后台设置摘要: 登录DedeCMS后台,进入“[核心]” -> “[内容模型管理]”,选择你的文章模型(默认是“文章”),点击“字段管理”。 确保“字段是启用状态,然后在发布或编辑文章时,在“文本框中输入你想要在首页显示的内容。
-
首页模板调用: 打开你的首页模板文件
index.htm,在需要显示文章列表的地方,找到DedeCMS的列表标签{dede:arclist}。在
{dede:arclist}标签中,使用[field:description/]来调用摘要。
示例代码:
{dede:arclist typeid='' row='10' titlelen='40' orderby='pubdate'}
<div class="article-item">
<h2><a href="[field:arcurl/]">[field:title/]</a></h2>
<div class="summary">
[field:description/]... <a href="[field:arcurl/]">[详情]</a>
</div>
<div class="info">
<span>发布时间:[field:pubdate function="MyDate('Y-m-d',@me)"/]</span>
<span>浏览:[field:click/]</span>
</div>
</div>
{/dede:arclist}
代码解释:
{dede:arclist}:文章列表调用标签。typeid='':指定栏目ID,空表示调用所有栏目。row='10':调用10篇文章,len='40'`:标题长度限制为40个字符。orderby='pubdate':按发布时间排序。[field:arcurl/]:文章链接。[field:title/]。[field:description/]:文章摘要,这是我们要调用的核心。[field:pubdate function="MyDate('Y-m-d',@me)"/]:格式化发布日期。[field:click/]:文章点击量。
自动截取文章正文内容(不推荐,但常见)
如果你没有填写摘要,又想在首页显示文章的开头部分,可以通过自动截取正文的办法来实现。
警告:这种方法有明显的缺点:
- 性能较差:需要读取完整的文章内容(
body字段),再进行截取,对首页加载速度有影响。 - 可能格式错乱:如果正文包含HTML标签(如
<p>、<img>等),直接截断可能会导致HTML标签不闭合,破坏页面样式。 - 内容冗余:如果文章开头是图片、代码块或无关紧要的引导语,截取出来的内容可能不是你想要的。
操作步骤:
-
修改
include/common.func.php文件: 这是DedeCMS的核心函数库,我们需要在这里添加一个自定义函数来安全地截取HTML内容。打开
include/common.func.php文件,在?>标签之前,添加以下函数:/** * 截取带有HTML标签的字符串,并保持标签闭合 * @param string $string 需要截取的字符串 * @param int $length 截取长度 * @param string $ellipsis 后缀 * @return string */ function html_cut($string, $length, $ellipsis = '...') { $string = strip_tags($string, '<p><br><strong><em><u><span>'); // 允许保留的标签,可以根据需要增删 $mb_strlen = mb_strlen($string, 'utf-8'); if ($mb_strlen <= $length) { return $string; } $string = mb_substr($string, 0, $length, 'utf-8'); // 简单处理标签闭合,非100%准确,但能满足大部分需求 preg_match_all('/<[^>]+>([^<]*)/', $string, $matches); $open_tags = array(); foreach ($matches[0] as $match) { if (preg_match('/^<(\w+)[^>]*>.*?<\/\1>$/s', $match)) { // 标签已闭合,忽略 continue; } if (preg_match('/^<(\w+)[^>]*>$/s', $match, $tag_match)) { $open_tags[] = $tag_match[1]; } elseif (preg_match('/^<\/(\w+)>$/s', $match, $tag_match)) { $key = array_search($tag_match[1], $open_tags); if ($key !== false) { unset($open_tags[$key]); } } } foreach ($open_tags as $tag) { $string .= "</{$tag}>"; } return $string . $ellipsis; } -
在首页模板中调用: 你可以在
{dede:arclist}标签中使用这个新函数。
示例代码:
{dede:arclist typeid='' row='10' titlelen='40' orderby='pubdate'}
<div class="article-item">
<h2><a href="[field:arcurl/]">[field:title/]</a></h2>
<div class="content-preview">
[field:body function='html_cut(@me, 200, "...")'/] <a href="[field:arcurl/]">[详情]</a>
</div>
<div class="info">
<span>发布时间:[field:pubdate function="MyDate('Y-m-d',@me)"/]</span>
<span>浏览:[field:click/]</span>
</div>
</div>
{/dede:arclist}
代码解释:
[field:body]:获取文章的完整正文内容。function='html_cut(@me, 200, "...")':调用我们刚刚在common.func.php中定义的html_cut函数。@me:代表当前字段的值,即[field:body]的内容。200:截取200个字符。- 截取后添加的后缀。
调用文章全文(极不推荐)
直接调用文章全文到首页,会严重拖慢网站加载速度,并且会导致首页内容极其冗长,用户体验极差。除非你有特殊需求(比如一个单页网站),否则请绝对不要这样做。
操作步骤:
在首页模板的 {dede:arclist} 中直接使用 [field:body/]。
示例代码(仅作演示,请勿用于生产环境):
{dede:arclist typeid='' row='5' titlelen='40' orderby='pubdate'}
<div class="full-article">
<h2><a href="[field:arcurl/]">[field:title/]</a></h2>
<div class="content">
[field:body/]
</div>
</div>
{/dede:arclist}
总结与建议
| 方法 | 优点 | 缺点 | 推荐度 |
|---|---|---|---|
| 调用摘要 | 速度快、SEO友好、页面整洁 | 需要手动在后台填写摘要 | ⭐⭐⭐⭐⭐ (强烈推荐) |
| 截取正文 | 无需手动填写摘要,自动生成 | 性能差、可能格式错乱 | ⭐⭐ (仅在无法填写摘要时考虑) |
| 调用全文 | 简单直接 | 性能极差、用户体验极差 | ⭐ (不推荐,除非特殊需求) |
最佳实践:
养成在后台发布文章时填写摘要的好习惯,这不仅能让你在调用首页内容时游刃有余,还能提升网站的整体性能和SEO效果。
