dede:typechannel标签如何正确使用?

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

Of course! Let's break down dede:typechannel in detail. This is a fundamental but often misunderstood tag in the DedeCMS (织梦CMS) template system.

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

What is dede:typechannel?

At its core, dede:typechannel is a DedeCMS loop tag designed to display a list of channels (categories) that belong to a specific parent channel.

Think of it as a way to programmatically generate a navigation menu, a list of sub-categories, or any list of categories within a predefined parent.


The Core Logic: How it Works

The tag's behavior is controlled by two main attributes:

  1. typeid (The Parent ID): This is the most crucial attribute. You must provide the ID of the parent channel. The tag will then fetch and loop through all immediate children of that parent. If you don't set typeid, it will often default to the ID of the current channel, which can be confusing.

    dede type channel
    (图片来源网络,侵删)
  2. noself (Include or Exclude Self): This is a boolean attribute (yes or no) that determines whether to include the parent channel itself in the list.

    • noself='yes' (default): Does NOT include the parent channel. It only shows its children.
    • noself='no': Includes the parent channel as the first item in the list, followed by its children.

Syntax and Parameters

The basic syntax is:

{dede:typechannel typeid='...' noself='...'}
    <!-- HTML and other Dede tags to display each channel -->
{/dede:typechannel}

Key Parameters:

Parameter Required? Default Value Description
typeid Yes (None) The ID of the parent channel whose children you want to list.
noself No yes yes: Do not include the parent channel. no: Include the parent channel.
row No 8 The number of channels to display.
cacheid No (None) The cache ID. If set, the results will be cached for performance.

Available Fields (Variables) Inside the Loop

When you use dede:typechannel, you are inside a loop. For each channel it fetches, you can access its information using specific field variables. These are prefixed with @me or arcurl (for links).

Here are the most common fields:

Field Name Description Example Usage
@me.id The ID of the current channel in the loop. <a href="{@me.id}">ID: {@me.id}</a>
@me.typename The name of the current channel. <a href="{@me.typeurl}">{@me.typename}</a>
@me.typeurl The direct URL link to the channel's list page. This is the most common field for links.
@me.description The description of the channel. <p>{@me.description}</p>
@me.seotitle The SEO title of the channel. <title>{@me.seotitle}</title>
@me.keywords The SEO keywords of the channel. <meta name="keywords" content="{@me.keywords}">
@me.content The introtext or description content of the channel. <div>{@me.content}</div>
@me.ispart 1 if it's a external link channel, 0 otherwise. [@me.ispart == 1] (for conditional logic)
@me.nok 1 if the channel is not to be displayed, 0 otherwise. [@me.nok == 0] (for conditional logic)

Practical Examples

Let's assume you have the following channel structure in your DedeCMS backend:

  • Home (ID: 1)
  • Products (ID: 2)
    • Laptops (ID: 5)
    • Phones (ID: 6)
    • Tablets (ID: 7)
  • News (ID: 3)
  • About Us (ID: 4)

Example 1: Basic Sub-Category List

Goal: On the "Products" page (ID: 2), list its sub-categories (Laptops, Phones, Tablets).

<h2>Our Product Categories</h2>
<ul>
    {dede:typechannel typeid='2' noself='yes'}
        <li>
            <a href="{@me.typeurl}">{@me.typename}</a>
        </li>
    {/dede:typechannel}
</ul>

Resulting HTML:

<h2>Our Product Categories</h2>
<ul>
    <li><a href="/plus/list.php?tid=5">Laptops</a></li>
    <li><a href="/plus/list.php?tid=6">Phones</a></li>
    <li><a href="/plus/list.php?tid=7">Tablets</a></li>
</ul>

Example 2: Including the Parent Channel ("Breadcrumbs" Style)

Goal: Create a list that includes "Products" and then its children.

<h2>Browse Our Products</h2>
<ul>
    {dede:typechannel typeid='2' noself='no'}
        <li>
            <a href="{@me.typeurl}">{@me.typename}</a>
        </li>
    {/dede:typechannel}
</ul>

Resulting HTML:

<h2>Browse Our Products</h2>
<ul>
    <li><a href="/plus/list.php?tid=2">Products</a></li>
    <li><a href="/plus/list.php?tid=5">Laptops</a></li>
    <li><a href="/plus/list.php?tid=6">Phones</a></li>
    <li><a href="/plus/list.php?tid=7">Tablets</a></li>
</ul>

Example 3: Displaying Only 2 Items

Goal: Show only the first two sub-categories of "Products".

<h2>Featured Categories</h2>
<ul>
    {dede:typechannel typeid='2' row='2'}
        <li>
            <a href="{@me.typeurl}">{@me.typename}</a>
        </li>
    {/dede:typechannel}
</ul>

Resulting HTML:

<h2>Featured Categories</h2>
<ul>
    <li><a href="/plus/list.php?tid=5">Laptops</a></li>
    <li><a href="/plus/list.php?tid=6">Phones</a></li>
</ul>

Common Pitfalls and Troubleshooting

  1. "It's not showing anything!"

    • Most likely reason: You forgot the typeid attribute, or the typeid you provided is wrong.
    • Solution: Double-check the ID of your parent channel in the DedeCMS channel management section ([你的域名]/dede/content_list.php?channelid=1).
  2. "It's showing the wrong channels!"

    • Reason: The typeid is pointing to a different parent than you intended.
    • Solution: Carefully verify the channel ID.
  3. "Why is it showing the current page's name in the list?"

    • Reason: You either did not provide a typeid, or noself is set to 'no' when you didn't want it.
    • Solution: If you only want children, ensure you have typeid='X' and noself='yes' (or just omit noself as it's the default).
  4. "The link is broken or goes to the wrong place."

    • Reason: You are using the wrong field. @me.id is just the number, not a URL. You must use @me.typeurl for the link.
    • Solution: Replace {@me.id} with {@me.typeurl} in your <a> tag's href attribute.

When to Use dede:typechannel vs. dede:channel

This is a key distinction for DedeCMS developers.

Feature dede:typechannel dede:channel
Purpose Lists children of a specific parent. Lists top-level channels or all channels from a root.
Primary Use Sub-menus, category lists within a parent page. Main site navigation, primary category menu.
Control Controlled by typeid (the parent). Controlled by typeid (the root) and reid (the parent).
Typical Use Case {dede:typechannel typeid='2'} {dede:typechannel typeid='0'} (to get all top-level channels)

In short:

  • Use dede:typechannel when you are inside a category page and want to list its sub-categories.
  • Use dede:channel (often with typeid='0') when you are building the main navigation menu for your entire site.
-- 展开阅读全文 --
头像
C语言为何如此有趣?
« 上一篇 2025-12-19
织梦CMS系统基本参数为何空白?
下一篇 » 2025-12-19

相关文章

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

目录[+]