dede channel id是什么?如何获取与使用?

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

Of course. This is a very common question for developers working with DedeCMS (织梦内容管理系统).

Let's break down what "dede channel id" means, how to find it, and how to use it.

What is a Dede Channel ID?

In DedeCMS, a Channel (频道) is a top-level category for your content. It's the main container for organizing your articles, products, downloads, etc. Think of it like the main sections of a website:

  • Home (首页)
  • News (新闻)
  • Products (产品)
  • Downloads (下载)
  • Pictures (图片)

Each of these main channels has a unique Channel ID, which is a number stored in the database. This ID is crucial because:

  1. Database Relationship: It's the primary key in the dede_arctype table (channel type table) and a foreign key in the dede_archives table (content table). It links a specific piece of content (an article) to its parent channel.
  2. Template Development: You often need the channel ID to call specific content in your templates using Dede's built-in tags like {dede:channel} or {dede:arclist}.
  3. PHP Development: In custom PHP scripts, you query the database using this ID to fetch content belonging to a specific channel.

How to Find a Channel ID (3 Easy Methods)

Here are the most common ways to find the Channel ID you need.

Method 1: The Easiest Way - From the URL (for Backend Management)

This is the quickest method if you just need the ID for a channel you are already managing.

  1. Log in to your DedeCMS backend (admin panel).

  2. Navigate to the channel you want to know the ID for. For example, go to [Content] -> [Manage All Channels].

  3. Find the channel in the list and click its "Manage Content" (内容管理) or "Add Content" (添加内容) link.

  4. Look at the URL in your browser's address bar. It will contain the cid parameter.

    Example URL: http://your-site.com/dede/content_list.php?cid=8

    In this example, the Channel ID is 8.

Method 2: The Most Reliable Way - Directly in the Database

This method gives you a complete list of all channels and their IDs, which is very useful for programming.

  1. Access your website's database using a tool like phpMyAdmin.
  2. Select the DedeCMS database (usually starts with dede_ or your-site-prefix_).
  3. Look for the table named dede_arctype. (Note: In some older versions, it might be dede_channeltype).
  4. Browse the contents of this table. You will see several columns:
tid (or id) reid topid typename
1 0 0 Home (首页)
2 0 0 News (新闻)
6 0 0 Products (产品)
8 0 0 Downloads (下载)
  • The tid (or id) column is the Channel ID.
  • The typename column is the name of the channel.
  • Channels with a reid of 0 are the main top-level channels.

Method 3: The Developer Way - Using a PHP Script

If you are writing a custom PHP file and need to get the ID dynamically, you can query the database directly.

This example connects to the database and fetches all top-level channels and their IDs.

<?php
// --- Database Configuration ---
$dbhost = 'localhost';
$dbuser = 'your_db_user';
$dbpass = 'your_db_password';
$dbname = 'your_db_name';
$prefix = 'dede_'; // Your table prefix
// --- Connect to Database ---
$conn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
// --- Set Character Set to UTF-8 ---
mysqli_set_charset($conn, "utf8");
// --- SQL Query to get Top-Level Channels ---
$sql = "SELECT tid, typename FROM {$prefix}arctype WHERE reid = 0 ORDER BY tid ASC";
$result = mysqli_query($conn, $sql);
// --- Display the Results ---
if (mysqli_num_rows($result) > 0) {
    echo "<h2>DedeCMS Channel IDs:</h2>";
    echo "<ul>";
    while($row = mysqli_fetch_assoc($result)) {
        // Use htmlspecialchars for security
        echo "<li>ID: " . htmlspecialchars($row["tid"]) . " - Name: " . htmlspecialchars($row["typename"]) . "</li>";
    }
    echo "</ul>";
} else {
    echo "No channels found.";
}
// --- Close the Connection ---
mysqli_close($conn);
?>

How to Use the Channel ID

In a Template File (.htm)

You can use the {dede:channel} tag to list sub-channels of a specific parent channel.

Example: To list all sub-channels under the main "News" channel (assuming its ID is 2):

{dede:channel type='son' typeid='2'}
    <a href="[field:typelink/]">[field:typename/]</a>
{/dede:channel}

In a PHP File

You use the ID in your SQL WHERE clause to filter content.

Example: To get all articles from the "Products" channel (assuming its ID is 6):

<?php
// (Database connection code from above example goes here)
$channel_id_to_get = 6; // The ID of the 'Products' channel
$sql = "SELECT * FROM {$prefix}archives WHERE typeid = {$channel_id_to_get} AND arcrank > -1 ORDER BY pubdate DESC LIMIT 10";
$result = mysqli_query($conn, $sql);
// Loop through and display the articles
while($row = mysqli_fetch_assoc($result)) {
    echo "<h3>" . htmlspecialchars($row['title']) . "</h3>";
    echo "<p>" . htmlspecialchars($row['description']) . "</p>";
}
// (Close connection code goes here)
?>

Summary

Method When to Use How to Find It
URL Parameter Quick lookup in the admin panel. Look for cid=XX in the URL when managing channel content.
Database Table Programming, getting a full list, verification. Query the dede_arctype table and look at the tid column.
PHP Script Dynamic content retrieval in custom code. Use SELECT tid, typename FROM dede_arctype WHERE reid = 0.
-- 展开阅读全文 --
头像
dede admin密码忘记怎么办?
« 上一篇 01-09
织梦404模板下载,哪里找免费好用的?
下一篇 » 01-09

相关文章

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

目录[+]