dede list orderby如何按指定字段排序?

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

Of course! Let's dive deep into the orderby parameter in DedeCMS (织梦CMS) for the list tag. This is one of the most important and frequently used parameters for controlling the order of articles on a list page (like an article list, product list, etc.).

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

What is orderby?

The orderby parameter in the {dede:list} tag tells DedeCMS which field to use as the basis for sorting the articles on the page. It determines the "order by" clause in the underlying SQL query.


Basic Syntax

The orderby parameter is placed inside the {dede:list} tag, like this:

{dede:list orderby='your_order_field' pagesize='20'}
    <a href='[field:arcurl/]'>[field:title/]</a>
    <span>[field:pubdate function="MyDate('Y-m-d',@me)"/]</span>
{/dede:list}

Common orderby Values and Their Meanings

Here is a list of the most common and useful values you can use for orderby.

Value Description Sorting Direction Notes
default Default Order. This is usually the order in which articles were added to the database, often corresponding to their id. Ascending This is the fallback if no orderby is specified. It's not very predictable for display purposes.
id Sorts by the article's unique ID. Ascending (older to newer) or Descending (newer to older) Very reliable for getting a consistent order.
pubdate Sorts by the article's publication date. Ascending (oldest to newest) or Descending (newest to oldest) This is the most common value for creating a chronological blog or news feed.
sortrank Sorts by the manual sorting value set in the article's backend. Ascending or Descending This is extremely powerful. You can assign a number to each article (e.g., 1, 2, 3...) to control their exact display order manually.
click Sorts by the number of clicks/views the article has received. Descending (highest to lowest) or Ascending (lowest to highest) Perfect for creating "Most Popular" or "Trending" article lists.
lastpost Sorts by the date of the last comment on the article. Ascending or Descending Useful for highlighting "Most Discussed" content.
scores Sorts by the article's score (if a rating system is enabled). Descending (highest score to lowest) Good for "Top Rated" lists.
rand Random Order. Sorts articles in a completely random order every time the page is loaded. N/A Excellent for displaying "Featured Articles" or "Random Picks" to keep the content fresh.

Combining orderby with orderway

To specify the direction of the sort (ascending or descending), you use the orderway parameter.

dede list orderby
(图片来源网络,侵删)
  • orderway='asc' for Ascending (A-Z, 0-9, Oldest-Newest)
  • orderway='desc' for Descending (Z-A, 9-0, Newest-Oldest)

Examples:

  1. Newest Articles First (Most Common) Sort by publication date, newest on top.

    {dede:list orderby='pubdate' orderway='desc' pagesize='10'}
        <!-- ... -->
    {/dede:list}
  2. Most Popular Articles Sort by click count, highest on top.

    {dede:list orderby='click' orderway='desc' pagesize='10'}
        <!-- ... -->
    {/dede:list}
  3. Manual Order (Editor's Choice) Sort by the sortrank field, with the lowest number appearing first (e.g., rank 1, then 2, then 3).

    {dede:list orderby='sortrank' orderway='asc' pagesize='10'}
        <!-- ... -->
    {/dede:list}
  4. Random Featured Article Display one article in a random order. This is great for a "Featured Article" section on the homepage.

    dede list orderby
    (图片来源网络,侵删)
    {dede:list orderby='rand' pagesize='1'}
        <h2>Featured Article</h2>
        <a href="[field:arcurl/]">[field:title/]</a>
    {/dede:list}

Advanced: Custom Sorting with orderby and a Custom Field

Sometimes you need to sort by a custom field that you've added to your articles (e.g., price, author_weight, event_date).

Step 1: Add the Custom Field Make sure you have added the custom field to your DedeCMS channel (e.g., in "Content Model Management").

Step 2: Use the Field Name in orderby You can directly use the name of your custom field in the orderby parameter.

Example: Sorting by a "Price" Custom Field

Let's say you have a custom field named price (with the variable name price).

{dede:list orderby='price' orderway='asc' pagesize='20'}
    <a href='[field:arcurl/]'>[field:title/]</a>
    <span>Price: ¥[field:price/]</span>
{/dede:list}

This will sort all articles by their price value in ascending order (from lowest to highest price).

Note: For this to work reliably, ensure all articles in that list channel have a value for the price field, or your database might return unexpected results.


Troubleshooting & Best Practices

  • Check Your Channel: The available sorting fields depend on the content model of your channel. For example, lastpost is only relevant if comments are enabled.
  • sortrank is Your Friend: For any list where you need manual control, always use orderby='sortrank'. It gives you editorial control without being dependent on dates or clicks.
  • Performance: Sorting by click or rand can be slower on very large sites because they require more complex database calculations. For high-traffic sites, using id or pubdate is generally the most performant.
  • Caching: Remember that DedeCMS uses page caching. If you use orderby='rand', the random order will be the same for all users until the cache is cleared. To get a truly random experience for each user, you might need to disable caching for that specific list tag or use a more advanced technique.
-- 展开阅读全文 --
头像
dede pagelist不显示怎么办?
« 上一篇 2025-12-19
dede arclist body标签如何调用文章内容?
下一篇 » 2025-12-19

相关文章

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

目录[+]