Of course! This is a very common and fundamental concept in DedeCMS (织梦内容管理系统). Let's break down what dede.field.id is, what it does, and how to use it correctly.
What is {dede:field.id}?
In simple terms, {dede:field.id} is a DedeCMS global field tag that is used within an article's template (usually article_article.htm) to display the unique ID number of the currently viewed article.
Think of it as a placeholder that DedeCMS automatically replaces with the specific ID of the content being displayed.
{dede:: This is the standard opening syntax for all DedeCMS tags.field: This indicates that we are accessing a field of the current content item.id: This specifies which field we want. In this case, the primary key or unique identifier for the article in the database.- : This closes the tag.
Where is it Used?
You will almost exclusively use {dede:field.id} inside the article detail page template. This template is responsible for displaying the full content of a single news article, document, or product.
Common template filenames for this are:
article_article.htm(most common for articles)article_spec.htm(for articles with special specifications)diy_list.htm(in DIY mode)
How to Use It: A Practical Example
Let's say you want to display the article's ID number on its own page, perhaps for debugging or for internal reference.
-
Open your article template file. Navigate to the DedeCMS admin panel, go to
模板->默认模板管理->文章and editarticle_article.htm. -
Add the tag. You can place it anywhere you want the ID to appear. For example, you might add it at the top of the page in a comment or a small, hidden div.
<html> <head> <title>{dede:field.title/} - {dede:global.cfg_webname/}</title> </head> <body> <!-- This is a great place to see the article ID for debugging --> <div style="display:none; color: #999;"> Article ID: {dede:field.id/} </div> <h1>{dede:field.title/}</h1> <div class="info"> 发布时间:{dede:field.pubdate function="MyDate('Y-m-d H:i',@me)"/} 作者:{dede:field.writer/} 来源:{dede:field.source/} </div> <div class="content"> {dede:field.body/} </div> </body> </html>
When a user visits an article, DedeCMS will process this template. If the article they are viewing has an ID of 154, the final HTML output on the user's browser will be:
<html>
<head>如何使用DedeCMS - 我的网站</title>
</head>
<body>
<!-- This is a great place to see the article ID for debugging -->
<div style="display:none; color: #999;">
Article ID: 154
</div>
<h1>如何使用DedeCMS</h1>
...
Advanced Usage: Passing the ID to Other Scripts
The real power of {dede:field.id} isn't just displaying it, but using its value. You can pass this ID to other PHP scripts, JavaScript, or form actions to perform dynamic actions related to that specific article.
Example 1: Passing the ID to a PHP Script
Imagine you have a custom PHP script (like.php) that handles "liking" an article. You need to tell it which article to like.
In your article_article.htm template, you can create a link and pass the ID as a parameter:
<a href="/plus/like.php?id={dede:field.id/}">喜欢这篇文章</a>
When a user clicks this link, the URL generated will be something like /plus/like.php?id=154. Your like.php script can then receive 154 via the $_GET['id'] variable and update the database accordingly.
Example 2: Using the ID in JavaScript
Let's say you want to load comments for the article using AJAX. You need the article's ID to fetch the correct comments.
<div id="comments-container">
<!-- Comments will be loaded here by JavaScript -->
</div>
<script>
// Get the article ID from the DedeCMS tag
var articleId = "{dede:field.id/}";
// Use it in a function to fetch comments
function loadComments() {
fetch("/get_comments.php?article_id=" + articleId)
.then(response => response.json())
.then(data => {
// ... process and display the comments ...
document.getElementById('comments-container').innerHTML = data.html;
});
}
// Load comments when the page is ready
window.onload = loadComments;
</script>
Important Note on JavaScript: In the example above, {dede:field.id/} is parsed by DedeCMS on the server before the page is sent to the browser. The resulting JavaScript variable articleId will be a plain number, like 154, not the {dede:field.id/} string.
Common Pitfalls and Important Notes
-
Scope:
{dede:field.id/}only works on the article detail page. If you try to use it on the homepage (index.htm), a list page (list_article.htm), or a search results page, it will either be empty or cause an error, because there is no single "current article" in those contexts. -
Not for List Pages: On a list page (e.g.,
list_article.htm), you are looping through many articles. To get the ID of each article in the loop, you must use the loop tag syntax:{dede:list}{/dede:list}.{dede:list} <li> <a href="[field:arcurl/]">[field:title/]</a> <!-- The ID of each article in the list --> (ID: [field:id/]) </li> {/dede:list}Notice the difference in syntax:
[field:id/]inside the loop vs.{dede:field.id/}on the detail page. -
Security: When you use
{dede:field.id/}to pass an ID to a script (like in thelike.phpexample), always sanitize the input in your receiving script. Treat it as untrusted data to prevent potential SQL injection or other security vulnerabilities.// In like.php $article_id = isset($_GET['id']) ? (int)$_GET['id'] : 0; // Cast to an integer if ($article_id > 0) { // Now it's safer to use $article_id in your database query // $sql = "UPDATE dede_archives SET scores = scores + 1 WHERE id = " . $article_id; }
Summary
| Tag | Syntax | Location | Purpose |
|---|---|---|---|
| Article ID (Detail Page) | {dede:field.id/} |
article_article.htm |
To get the ID of the single article being viewed. |
| Article ID (List Page) | [field:id/] |
Inside {dede:list} loop |
To get the ID of each article in a list of articles. |
{dede:field.id} is a simple but incredibly powerful tool that forms the backbone of dynamic, individual content handling in DedeCMS.
