dede arclist orderby参数如何正确使用?

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

Of course! The orderby parameter is one of the most important and frequently used attributes in the DedeCMS {dede:arclist} tag. It controls the order in which articles are listed.

dede arclist orderby
(图片来源网络,侵删)

Here is a comprehensive guide to using orderby in dede:arclist, covering all the common and advanced options.


Basic Syntax

The orderby attribute is placed inside the {dede:arclist} tag.

{dede:arclist orderby='排序字段' limit='0,10'}
    <li>
        <a href="[field:arcurl/]">[field:title/]</a>
        <span>[field:pubdate function="MyDate('Y-m-d',@me)"/]</span>
    </li>
{/dede:arclist}

Common orderby Values

These are the values you'll use 95% of the time.

Value Description Example
default Default order. This is a special value that combines sortrank (manual order) and pubdate (publication date). It first shows articles manually ordered, then the rest by publication date. orderby='default'
pubdate Publication Date. Orders articles by when they were published, from newest to oldest. This is the most common alternative to default. orderby='pubdate'
id Article ID. Orders articles by their unique numeric ID, from highest to lowest (newer articles usually have higher IDs). orderby='id'
sortrank Manual Order. Orders articles by the manual order value set in the article's editing interface (under "Advanced Options"). Lower numbers appear first. orderby='sortrank'
click Click Count. Orders articles by the number of times they have been viewed, from highest to lowest. orderby='click'
lastpost Last Post Time. Orders by the time of the last comment/reply on the article. orderby='lastpost'
scores Article Score. Orders by the score/rating of the article (if a rating system is enabled). orderby='scores'

Advanced Usage

You can make your orderby queries much more powerful by combining fields.

dede arclist orderby
(图片来源网络,侵删)

a) Sorting Direction (ASC/DESC)

By default, most orders are descending (newest, highest, etc.). You can add ASC to make it ascending (oldest, lowest, etc.).

Syntax: orderby='字段 ASC' or orderby='字段 DESC'

Examples:

  • Order by publication date, oldest first:

    dede arclist orderby
    (图片来源网络,侵删)
    {dede:arclist orderby='pubdate ASC' ...}
  • Order by article ID, lowest first (oldest articles):

    {dede:arclist orderby='id ASC' ...}
  • Order by click count, lowest first:

    {dede:arclist orderby='click ASC' ...}

b) Multiple Fields (Composite Sorting)

You can sort by more than one field at a time by separating them with commas. This is extremely useful for creating complex lists.

Syntax: orderby='字段1, 字段2'

Examples:

  • Sort by manual order first, then by publication date for ties: This is a very common pattern. It ensures that articles with the same manual order are then shown in reverse chronological order.

    {dede:arclist orderby='sortrank, pubdate DESC' ...}
  • Sort by click count, but for articles with the same clicks, show the newest ones first:

    {dede:arclist orderby='click, pubdate DESC' ...}
  • Sort by channel ID, then by manual order within that channel:

    {dede:arclist orderby='typeid, sortrank' ...}

Practical Examples

Let's put it all together with some common use cases.

Example 1: A "Latest News" Section

You want to show the 10 most recently published articles.

<h2>Latest News</h2>
<ul>
    {dede:arclist titlelen='30' row='10' orderby='pubdate'}
        <li>
            <span class="date">[field:pubdate function="MyDate('m-d',@me)"/]</span>
            <a href="[field:arcurl/]" title="[field:title/]">[field:title/]</a>
        </li>
    {/dede:arclist}
</ul>
  • orderby='pubdate' ensures the newest articles are at the top.

Example 2: A "Popular Articles" Sidebar

You want to show the 5 most-viewed articles from a specific category (ID=5).

<h2>Popular Reads</h2>
<ol>
    {dede:arclist typeid='5' titlelen='26' row='5' orderby='click'}
        <li><a href="[field:arcurl/]" title="[field:title/]">[field:title/]</a></li>
    {/dede:arclist}
</ol>
  • typeid='5' limits the list to category 5.
  • orderby='click' orders by view count.

Example 3: A "Mixed" List with Manual Override

You want a list where you can manually pin important articles to the top, and the rest follow in reverse date order. This is the power of default, but let's be explicit.

<h2>Featured & Latest</h2>
<ul>
    {dede:arclist titlelen='40' row='15' orderby='sortrank, pubdate DESC'}
        <li>
            <a href="[field:arcurl/]" title="[field:title/]">[field:title/]</a>
            <em>Manual Order: [field:sortrank/]</em>
        </li>
    {/dede:arclist}
</ul>
  • orderby='sortrank, pubdate DESC' is the key. It first sorts by the manual sortrank (lower numbers are first). If two articles have the same sortrank, it then sorts them by pubdate in descending order (newest first).

Important Considerations

  1. Performance: Sorting by click or scores can be slower on very large sites because it may require more complex database calculations. Sorting by id, pubdate, or sortrank is much faster.
  2. The default Order: It's crucial to understand that orderby='default' is a special, built-in DedeCMS order. It's equivalent to ORDER BY IF(sortrank > 0, sortrank, 9999999), pubdate DESC. This is why it's so effective for combining manual and automatic ordering.
  3. Field Case Sensitivity: DedeCMS field names are generally case-insensitive, but it's good practice to use the same case as the documentation (e.g., pubdate, not PubDate).

By mastering the orderby attribute, you gain precise control over how content is displayed on your DedeCMS website, allowing you to create everything from simple blogrolls to complex, custom-featured content sections.

-- 展开阅读全文 --
头像
C语言createemptylist如何创建空列表?
« 上一篇 2025-12-02
2025年dedecms织梦手机站教程
下一篇 » 2025-12-02

相关文章

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

目录[+]