dede list标签如何正确调用与使用?

99ANYc3cd6
预计阅读时长 32 分钟
位置: 首页 DEDE建站 正文

Of course! The dede:list tag is one of the most fundamental and frequently used tags in the DedeCMS (织梦CMS) template system. Its primary purpose is to display a list of articles, typically on a channel homepage, a list page, or a search results page.

Here's a comprehensive breakdown of the dede:list tag, covering its basic usage, parameters, and best practices.


Basic Syntax and Purpose

The dede:list tag is a loop tag, meaning it repeats a block of HTML for each item it retrieves from the database.

Core Purpose:

  • To fetch a specific set of articles from a designated channel (or栏目).
  • To loop through these articles and display them using a custom HTML template for each one.

Basic Structure:

{dede:list}
    <!-- This HTML block will be repeated for each article -->
    <li>
        <a href="[field:arcurl/]">[field:title/]</a>
        <span>[field:pubdate function="MyDate('Y-m-d', @me)"/]</span>
    </li>
{/dede:list}

Key Parameters

You can control the behavior of dede:list by adding parameters inside the opening tag. These are placed after list, separated by commas and spaces.

a. typeid (栏目ID)

This is the most important parameter. It specifies which channel's articles to display.

  • Usage: {dede:list typeid='1'}
  • Description: Displays articles from the channel with ID 1.
  • Special Values:
    • typeid='0': Displays articles from all channels across the entire site. (Use with caution on large sites as it can be slow).
    • typeid='' (empty): If the current page is a channel list page itself, it will default to displaying articles from that channel. This is the most common usage.

b. row (显示条数 / Number of items to display)

This sets how many articles to show on the current page.

  • Usage: {dede:list row='10'}
  • Description: Displays the top 10 articles from the specified typeid.
  • Default: Usually 10, but this can be configured in the DedeCMS backend (系统 > 系统基本参数 > 核心设置).

c. titlelen (标题长度 / Title length)

This controls the maximum number of characters for the article title.

  • Usage: {dede:list titlelen='30'}
  • Description: If a title is longer than 30 characters, it will be truncated (usually with ).
  • Default: Often 30 or 40.

d. infolen (摘要长度 / Summary length)

This controls the length of the article's summary (description).

  • Usage: {dede:list infolen='100'}
  • Description: Displays the first 100 characters of the article's summary (description field).
  • Default: Often 160.

e. orderby (排序方式 / Order by)

This defines how the articles are sorted.

  • Usage: {dede:list orderby='pubdate'}
  • Common Values:
    • orderby='pubdate': Sort by publication date (newest first). This is the default.
    • orderby='hot' or orderby='click': Sort by the number of clicks/views (most popular first).
    • orderby='sortrank': Sort by manual sorting order set in the article backend.
    • orderby='id': Sort by the article's unique ID.
    • orderby='arcrank': Sort by article rank (e.g., featured articles first).

f. orderway (排序顺序 / Sort order)

This works with orderby to specify ascending or descending order.

  • Usage: {dede:list orderby='pubdate' orderway='desc'}
  • Values:
    • orderway='desc': Descending order (e.g., newest first, highest clicks first). This is the default.
    • orderway='asc': Ascending order (e.g., oldest first).

g. channelid (模型ID / Channel ID)

This is used if you are not listing articles from a standard "article" channel but from a custom channel type you've created (e.g., a "product" or "download" channel).

  • Usage: {dede:list channelid='2' ...}
  • Description: Lists items from the custom channel with ID 2. When using this, the available [field:...] variables will change to match the fields of that custom model.

The Essential Field Variables ([field:...])

Inside the {dede:list}...{/dede:list} block, you use special variables to display article information. These are prefixed with field:.

Variable Name Description Common Usage
[field:title/] The full title of the article. <a href="[field:arcurl/]">[field:title/]</a>
[field:arcurl/] The full URL to the article's detail page. <a href="[field:arcurl/]">Read more</a>
[field:pubdate/] The publication date (Unix timestamp). Needs a function to format.
[field:description/] The article's summary/description. [field:description function='cn_substr(@me, 100)'/]
[field:litpic/] The URL of the article's thumbnail or cover image. <img src="[field:litpic/]" alt="[field:title/]">
[field:click/] The number of times the article has been viewed. Viewed: [field:click/] times
[field:id/] The unique ID of the article. Useful for JavaScript or other custom logic.
[field:typename/] The name of the channel the article belongs to. Category: [field:typename/]
[field:shorttitle/] A custom short title for the article. [field:shorttitle/]

Formatting Dates with a Function

The [field:pubdate/] variable returns a number (a Unix timestamp). You must use a function to format it into a human-readable date.

Example:

{dede:list}
    <li>
        <a href="[field:arcurl/]">[field:title/]</a>
        <span>发布时间:[field:pubdate function="MyDate('Y-m-d H:i', @me)"/]</span>
    </li>
{/dede:list}
  • MyDate(): This is DedeCMS's built-in date formatting function.
  • 'Y-m-d H:i': This is the format string. Y=4-digit year, m=2-digit month, d=2-digit day, H=24-hour, i=minutes.
  • @me: This is a placeholder that represents the original value of the field (the timestamp).

Complete Practical Example

Let's imagine we have a "News" channel with ID 3. We want to create a list page for it that shows:

  • 15 articles per page.
  • Sorted by the most recent.
  • Each item shows the title, a thumbnail, the publication date, and a short summary.

Template Code (list_news.htm):

<!DOCTYPE html>
<html>
<head>{dede:field.title/}</title>
</head>
<body>
    <h1>{dede:field.title/}</h1>
    <ul class="news-list">
        {dede:list typeid='3' row='15' titlelen='40' infolen='120'}
            <li>
                <div class="news-thumb">
                    <a href="[field:arcurl/]">
                        <img src="[field:litpic/]" alt="[field:title/]" />
                    </a>
                </div>
                <div class="news-content">
                    <h2><a href="[field:arcurl/]">[field:title/]</a></h2>
                    <p class="news-summary">[field:description function='cn_substr(@me, 120)'/]</p>
                    <p class="news-meta">发布于:[field:pubdate function="MyDate('Y-m-d', @me)"]</p>
                </div>
            </li>
        {/dede:list}
    </ul>
    <!-- 分页标签 (Pagination Tag) -->
    <div class="dede_pages">
        <ul>
            {dede:pagelist listsize='4' listitem='info,index,end,pre,next,pageno'}
            </ul>
    </div>
</body>
</html>

dede:list vs. dede:arclist

This is a very common point of confusion. Both tags create article lists, but they are used in different contexts.

Feature dede:list dede:arclist
Primary Use List Pages. The main content of a channel's list page (e.g., /plus/list.php?tid=1). Templets. Used inside an article's detail page template (article_article.htm) or other custom templates to display related articles, recommended articles, etc.
Pagination Built-in & Automatic. It automatically handles pagination. The {dede:pagelist/} tag is its companion. Manual. You must manually handle pagination logic using GetPageList() or by passing pageno and pagesize parameters. It's more complex.
Context Operates within the context of a specific channel list page. Operates as a standalone tag that can pull articles from any channel, regardless of the current page's context.
Simplicity Simpler for its intended purpose (making list pages). More flexible but more complex for its intended purpose (making article lists within content).

Rule of Thumb:

  • If you are building the main listing page for a channel, use {dede:list}.
  • If you are inside an article's detail page and want to show "Related Articles" or "Latest News", use {dede:arclist}.
-- 展开阅读全文 --
头像
C语言x2012,版本特性与开发实践指南?
« 上一篇 今天
织梦搬家验证码不显示,如何解决?
下一篇 » 今天
取消
微信二维码
支付宝二维码

目录[+]