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

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

Of course! Let's dive deep into the dede:channel tag and its orderby attribute in DedeCMS (织梦CMS).

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

This is a fundamental and powerful tag used for displaying channel (category) lists, such as in your website's main navigation.


The dede:channel Tag Overview

The dede:channel tag is used to fetch and display a list of channels (categories) from the DedeCMS database. Its basic syntax is:

{dede:channel}
    <a href='[field:typeurl/]'>[field:typename/]</a>
{/dede:channel}

Inside the tag, you use field variables to display specific information for each channel, like:

  • [field:typename/]: The name of the channel.
  • [field:typeurl/]: The URL of the channel.
  • [field:id/]: The ID of the channel.
  • [field:soncount/]: The number of sub-channels.
  • [field:artcount/]: The number of articles in the channel.

The orderby Attribute

The orderby attribute is crucial because it determines the order in which the channels are fetched and displayed from the database. You can use it to sort by different properties like ID, name, article count, etc.

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

Here are the most common and useful values for orderby:

Value Description Example Use Case
id Sort by channel ID (default). This is the primary key in the dede_arctype table. The default behavior. Good for a simple, unsorted list.
sortrank Sort by manual order rank. This is the most common and recommended method for controlling the order of your main navigation. You set this value in the channel's backend settings. Main navigation menu. You manually assign sortrank values (e.g., 1, 2, 3...) to order your "Home", "About Us", "Products", "Contact" links.
typename Sort alphabetically by channel name. The channels are ordered from A to Z based on their [typename]. A glossary or an alphabetical list of services.
arcrank Sort by article count (number of articles). Channels with more articles appear first. A "Popular Sections" block on your homepage.
reid Sort by parent channel ID. This groups channels by their parent category. Useful for displaying sub-channels. Displaying a list of categories, with sub-categories grouped under each parent.
click Sort by click count. Channels that have been clicked more often appear first. (Requires click tracking to be enabled). A "Most Viewed Sections" feature.

Practical Examples

Let's see how to use orderby in common scenarios.

Example 1: Main Navigation (Manual Order)

This is the most typical use case. You want to control the order of your top-level menu items.

  1. In your DedeCMS backend, go to "Channel" -> "All Channels".

    dede channel orderby
    (图片来源网络,侵删)
  2. For each top-level channel (e.g., "Company News", "Product Center"), edit it and set the "Sort Rank" (sortrank) field. A lower number means it appears first. (e.g., Company News = 1, Product Center = 2, About Us = 3).

  3. In your template file (e.g., head.htm), use the following code:

{dede:channel type='top' orderby='sortrank' row='8'}
    <li><a href="[field:typeurl/]">[field:typename/]</a></li>
{/dede:channel}

Explanation:

  • type='top': This is another important attribute that tells DedeCMS to only show top-level channels (those without a parent).
  • orderby='sortrank': This is the key. It sorts the channels based on the manual rank you set in the backend.
  • row='8': Limits the output to 8 channels.

Example 2: Alphabetical List of Services

If you have a list of services and want them sorted alphabetically.

<h3>Our Services</h3>
<ul>
    {dede:channel typeid='2' orderby='typename'}
        <li><a href="[field:typeurl/]">[field:typename/]</a></li>
    {/dede:channel}
</ul>

Explanation:

  • typeid='2': Limits the query to only channels within the parent channel with ID 2 (e.g., a "Services" category).
  • orderby='typename': Sorts the resulting channels alphabetically by their name.

Example 3: Displaying Sub-Channels Under a Parent

You might want to show a main category and then list its sub-categories in a specific order.

<!-- Main Category -->
<h2>[field:typename/]</h2>
<!-- List of Sub-Channels, ordered by their manual sort rank -->
{dede:channel typeid='5' orderby='sortrank'}
    <a href="[field:typeurl/]">[field:typename/]</a> |
{/dede:channel}

Explanation:

  • typeid='5': This specifies the parent channel. The dede:channel tag will then list all child channels of ID 5.
  • orderby='sortrank': Orders these child channels according to their individual "Sort Rank" settings.

Combining orderby with Other Important Attributes

To get the most out of dede:channel, you'll often use it with other attributes:

  • row: Sets the maximum number of channels to display.

    {dede:channel row='5' orderby='sortrank'}...{/dede:channel}
  • typeid: Specifies the parent channel to list sub-channels from. If not set, it lists from the current channel or the root.

    {dede:channel typeid='10' orderby='typename'}...{/dede:channel}
  • type: Controls which channels to display.

    • type='top': Only top-level channels (no parent).
    • type='son': Only the immediate child channels of the typeid specified.
    • type='self': Only the channel specified by typeid itself (useful for getting info about one channel).
  • currentstyle: Extremely useful for navigation. It allows you to apply a special CSS class to the currently active channel link.

    {dede:channel type='top' orderby='sortrank' currentstyle="<li class='active'><a href='~typelink~'>~typename~</a></li>"}
        <li><a href="[field:typeurl/]">[field:typename/]</a></li>
    {/dede:channel}
    • ~typelink~ is replaced with [field:typeurl/].
    • ~typename~ is replaced with [field:typename/].
    • If the current page is within that channel, the <li class='active'>...</li> code will be used instead of the default one.

Summary

Your Goal Recommended orderby Value Key Attribute to Pair With
Main Navigation Menu orderby='sortrank' type='top'
Alphabetical List orderby='typename' typeid (to limit scope)
Most Popular Sections orderby='arcrank' (None, or typeid)
Display Sub-Channels orderby='sortrank' typeid (the parent's ID)

By mastering the orderby attribute and combining it with typeid, type, and currentstyle, you can have full control over how your channel and category lists are displayed throughout your DedeCMS website.

-- 展开阅读全文 --
头像
Linux下ActiveMQ如何用C语言实现消息交互?
« 上一篇 2025-12-02
dede runphp globals如何正确使用与配置?
下一篇 » 2025-12-02

相关文章

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

目录[+]