Of course. Let's break down what the dede_field function is in DedeCMS (织梦内容管理系统), what it does, and how to use it. This is a fundamental concept for anyone working with DedeCMS templates.

What is dede_field?
In simple terms, dede_field is a powerful PHP function used within DedeCMS templates to retrieve and display the value of any field from a content record.
Think of it as a universal key that can open any "box" (field) in your article's data. Whether it's the article title, the author, the publishing date, or a custom field you added yourself, dede_field can get it for you.
Why is it called "globe"?
The term "globe" in this context is likely a direct translation of the Chinese term 全局 (quánjú), which means "global" or "universal".
The function is called dede_field because it can access fields globally from the current content record being processed by the engine. It's not limited to specific template variables like {dede:field.title}. It can access any field defined in the dede_archives table (for articles) or related custom tables.

The Basic Syntax
The function is most commonly used within DedeCMS's template loop tags, like {dede:arclist} or {dede:loop}.
{dede:field name='field_name' /}
{dede:field ... /}: This is the opening and closing tag for the field function.name='field_name': This is the required attribute. You replacefield_namewith the actual name of the database field you want to retrieve.
How to Use dede_field: Common Examples
Let's assume you have a standard DedeCMS article. The main data is stored in the dede_archives table.
Example 1: Getting Standard Fields
These are fields that exist in every article by default.
-
Get the Article Title:
(图片来源网络,侵删){dede:field name='title' /}This is functionally identical to the more common
{dede:field.title /}. -
Get the Short Description (Introtext):
{dede:field name='description' /} -
Get the Full Content (Body):
{dede:field name='body' /} -
Get the Publishing Date (formatted):
{dede:field name='pubdate' function='MyDate(@me)' /}Here, we use the
functionattribute to format the timestamp.@meis a placeholder for the field's value. A more common built-in format isstrftime:{dede:field name='pubdate' function='strftime("%Y-%m-%d %H:%M:%S",@me)' /}
Example 2: Getting Custom Fields (The Power of dede_field)
This is where dede_field truly shines. Let's say you've added a custom field to your channel, for example, "Author's Website" (with the field name authorurl).
- Display the Custom Field:
作者网站: {dede:field name='authorurl' /}If the field is empty, this will still render the text "作者网站: ". To avoid this, you can use an
ifstatement.
Example 3: Using dede_field with Conditional Logic (if)
You often want to display something only if a custom field has a value. This is a very common and practical use case.
Let's display an author's website link only if the authorurl field is filled.
{dede:field name='authorurl' runphp='yes'}
if(@me != '') {
@me = "<a href='" . @me . "' target='_blank'>访问作者网站</a>";
} else {
@me = "";
}
{/dede:field}
Explanation of this code:
runphp='yes': This attribute tells DedeCMS to execute the PHP code inside the tag.if(@me != ''): It checks if the value of theauthorurlfield (@me) is not empty.@me = "...": If the condition is true, it reassigns the value of@meto be the HTML link.@me = "";: If the condition is false, it sets@meto an empty string, so nothing is displayed.
Comparison with Other Field Methods
It's helpful to see how dede_field fits in with other ways to get field data.
| Method | Syntax | When to Use | Scope |
|---|---|---|---|
dede_field |
{dede:field name='fieldname' /} |
Universal access. Use for any field, especially custom ones, or when inside complex loops. | Global to the current record. |
dede:field shortcut |
{dede:field.title /} |
Quick access to the most common fields of the current article (e.g., on an article detail page). | Limited to the main article fields. |
dede:arclist field |
{dede:arclist titlelen='30'}...{/dede:arclist} |
Accessing fields of articles within a list (like on the homepage or a category page). | Limited to the articles being listed. |
PHP in {dede:loop} |
{dede:loop table='...'}...[field:phpcode/]...{/dede:loop} |
When you need direct, raw PHP access to a record from a custom table. | Specific to the loop's table. |
Key Takeaways
- Universal Access:
dede_fieldis your go-to function for accessing any field from a content record in a DedeCMS template. - "Globe" = Global: It can access fields "globally" from the current data context, not just predefined ones.
- Essential for Custom Fields: It is the primary and most reliable way to display the values of custom fields you've added to your content types.
- Power with
runphp: Combiningdede_fieldwithrunphp='yes'gives you immense flexibility to format, manipulate, and conditionally display your data. - Know Your Field Name: To use it effectively, you must know the exact database field name (e.g.,
typeid,arcrank,your_custom_field_name). You can usually find these in the DedeCMS database or by examining the form when you edit an article.
