dede channellist

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

Of course! The dede:channellist tag is one of the most fundamental and frequently used tags in the DedeCMS (织梦CMS) template system. Its primary purpose is to generate a list of channels (also known as categories or sections) for your website's navigation menu.

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

Here’s a comprehensive breakdown of the dede:channellist tag, including its syntax, attributes, common usage examples, and important notes.


What is dede:channellist?

It's a DedeCMS template tag that loops through and displays a list of channels defined in your DedeCMS backend. These channels are typically the main sections of your website, like "Home," "News," "Products," "About Us," etc.

It's the go-to tag for building almost any kind of navigation menu, whether it's a horizontal top menu, a vertical sidebar menu, or a footer menu.

Basic Syntax

The tag must be used within a {dede:channellist} and {/dede:channellist} block.

dede channellist
(图片来源网络,侵删)
{dede:channellist}
    <a href='[field:typelink/]'>[field:typename/]</a>
{/dede:channellist}
  • [field:typelink/]: This is a "field tag" that gets replaced with the actual URL of the channel.
  • [field:typename/]: This field tag gets replaced with the name of the channel.

Key Attributes

Attributes control how the tag behaves. You can add them directly inside the opening {dede:channellist} tag.

a. typeid (Most Important Attribute)

This attribute specifies the parent channel ID from which to list its sub-channels. If you omit it, it will list all top-level channels (channels with a parent ID of 0).

  • Usage: To list all top-level channels (main menu):
    {dede:channellist}
        ...
    {/dede:channellist}
  • Usage: To list only the sub-channels of the "News" channel (assuming its ID is 2):
    {dede:channellist typeid='2'}
        ...
    {/dede:channellist}

b. type

This attribute determines the type of channels to be listed. The common values are:

  • top: Lists only top-level channels (same as not using typeid). This is the default.
  • son: Lists only the direct sub-channels of the channel specified in typeid.
  • self: Lists the channel specified in typeid and its direct sub-channels.

c. row

Specifies the maximum number of channels to display. Useful if you only want to show the first 5 items in a menu.

dede channellist
(图片来源网络,侵删)
  • Usage: Display only the first 3 channels.
    {dede:channellist row='3'}
        ...
    {/dede:channellist}

d. currentstyle

This is a very powerful attribute for styling the currently active channel. When a user is on a page within a specific channel, you can apply a special CSS class to that channel's link to make it stand out (e.g., make it bold or change its color).

  • Syntax: currentstyle='your_html_template'

  • [field:typename/] and [field:typelink/] can be used inside the currentstyle template.

  • Usage: Apply the class active to the link of the current channel.

    {dede:channellist currentstyle="<a href='[field:typelink/]' class='active'>[field:typename/]</a>"}
        <a href='[field:typelink/]'>[field:typename/]</a>
    {/dede:channellist}
    • How it works: If the current page belongs to one of the channels in the list, DedeCMS will use the currentstyle template instead of the default one for that specific item.

e. noself

If set to yes, it prevents the channel specified by typeid from being listed. It's useful when you only want to show its sub-channels.

  • Usage: List sub-channels of "News" (ID=2) but not "News" itself.
    {dede:channellist typeid='2' noself='yes'}
        ...
    {/dede:channellist}

Complete Field Tags (Variables)

Inside the {dede:channellist} loop, you can use the following field tags:

Field Tag Description
[field:id/] The ID of the channel.
[field:typename/] The name of the channel.
[field:typelink/] The full URL link to the channel's list page.
[field:typedir/] The directory path of the channel (e.g., /news/).
[field:seotitle/] The SEO title of the channel.
[field:description/] The description of the channel.

Practical Examples

Example 1: Basic Horizontal Top Menu

This code creates a simple, unordered list for a main navigation menu.

<ul id="main-nav">
    {dede:channellist}
        <li><a href="[field:typelink/]">[field:typename/]</a></li>
    {/dede:channellist}
</ul>

Example 2: Horizontal Menu with Active State

This is the most common real-world example. It highlights the current page in the menu.

<ul id="main-nav">
    {dede:channellist currentstyle="<li class='active'><a href='[field:typelink/]'>[field:typename/]</a></li>"}
        <li><a href="[field:typelink/]">[field:typename/]</a></li>
    {/dede:channellist}
</ul>

The generated HTML for the "News" page would look like this:

<ul id="main-nav">
    <li><a href="/">Home</a></li>
    <li class="active"><a href="/news/">News</a></li>
    <li><a href="/products/">Products</a></li>
    ...
</ul>

Example 3: Multi-Level Navigation (Dropdown Menu)

This requires using typeid to get sub-channels. The outer loop lists the top-level items, and the inner loop lists the children for each item.

<ul id="main-nav">
    {dede:channellist}
        <li>
            <a href="[field:typelink/]">[field:typename/]</a>
            {dede:channellist typeid='[field:id]' type='son'}
                <a href="[field:typelink/]">[field:typename/]</a>
            {/dede:channellist}
        </li>
    {/dede:channellist}
</ul>

Note: For more complex dropdowns, you might need to add conditional logic to only show the submenu if the parent has children.

Example 4: Sidebar Menu for a Specific Section

Let's say you have a "Products" section (with ID=3) and you want to list its sub-categories in the sidebar.

<div class="sidebar-menu">
    <h3>Our Products</h3>
    <ul>
        {dede:channellist typeid='3' noself='yes'}
            <li><a href="[field:typelink/]">[field:typename/]</a></li>
        {/dede:channellist}
    </ul>
</div>

Important Considerations

  • Channel Type: dede:channellist is designed to list channels that have their "List Article" option enabled. If you create a channel that is just a single page (an external link or a single document), it might not appear in a channellist unless you configure it specifically.
  • Caching: DedeCMS uses caching. If you make changes to channels in the backend, you may need to clear the site's cache (in the DedeCMS backend -> System -> Cache Settings) to see the changes reflected on the frontend immediately.
  • dede:channel vs. dede:channellist: Don't confuse this with dede:channel. The dede:channel tag is used to get information about a single channel, usually the current one. dede:channellist is for iterating through a list of channels.
-- 展开阅读全文 --
头像
c语言 什么意思
« 上一篇 2025-12-29
织梦v57_gbk_sp1
下一篇 » 2025-12-29

相关文章

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

目录[+]