Of course! Let's break down GetFieldValue in the context of DedeCMS (织梦CMS).

What is GetFieldValue?
GetFieldValue is a core PHP function in DedeCMS used to retrieve the value of a single, custom field from an article or other content item.
Think of it this way:
- An article in DedeCMS has standard fields like
title,description,body, etc. - You can add your own custom fields to an article's model (e.g.,
author_photo,related_download_link,video_url,product_price). GetFieldValueis the specific tool you use to fetch the value of just one of those custom fields in your template files (.htm).
The Function Signature
The function is typically called in two ways:
The Static Method (Most Common)
This is the standard way to call it within a DedeCMS template.

<?php echo GetFieldValue($arcRow, 'field_name'); ?>
The Object-Oriented Method
This is an alternative syntax that works in the same way.
<?php
echo $arcRow->GetFieldValue('field_name');
?>
Parameter Breakdown
Let's look at the parameters for the static method: GetFieldValue($arcRow, 'field_name', $isadd, $innertext)
| Parameter | Description | Example |
|---|---|---|
$arcRow |
(Required) This is the most important parameter. It's the PHP object that contains all the data for the current article (its title, ID, body, etc.). In DedeCMS loops (like arclist or list), this variable is typically named $fields or $row. |
$fields (from an arclist loop) |
'field_name' |
(Required) This is the exact name of the custom field you want to get the value from. This is case-sensitive! | 'author_photo' or 'product_price' |
$isadd |
(Optional) A boolean flag. It's used to determine if you are getting the value for a new article being added (true) or for an existing article (false). In 99% of template cases, you can leave this blank or set it to false. |
false |
$innertext |
(Optional) This is a powerful feature. If you provide this parameter, the function will treat the retrieved value as a template and process it. This allows for complex nested content. | <li><a href='{field_name}'>Download File</a></li> |
How to Use It: A Step-by-Step Example
Let's say you have an article model with a custom field called video_url.
Step 1: Ensure the Custom Field Exists
- Log in to your DedeCMS admin panel.
- Go to "Content" -> "Model Management" -> "Content Model".
- Click on "Add a new field" for your desired model (e.g., the default article model).
- Fill in the details:
- Field Name:
video_url(This must be unique and lowercase). - Field Type:
textortextarea. - Fill in other details like title, description, etc.
- Field Name:
- Save the model.
Step 2: Add Data to the Field
- Go to "Content" -> "Add Article".
- Fill in the article's title and body.
- Scroll down to the "Custom Document Fields" section.
- You will now see a field named
video_url. Enter a value for it, for example:https://www.youtube.com/watch?v=dQw4w9WgXcQ.
Step 3: Use GetFieldValue in the Template
Now, you want to display this video URL on your article detail page (article_article.htm).
Open your article_article.htm template file. You will need to find the loop that outputs the article's data. In DedeCMS, this is often an embedded PHP block.
<h1>{dede:field.title/}</h1>
<div class="content">
{dede:field.body/}
</div>
<!-- Now, let's add our custom field -->
<div class="video-section">
<h3>Related Video</h3>
<?php
// $arcRow is the variable holding the article's data in this context.
// 'video_url' is the name of our custom field.
$videoUrl = GetFieldValue($arcRow, 'video_url');
if (!empty($videoUrl)) {
echo '<iframe src="' . $videoUrl . '" width="560" height="315" frameborder="0" allowfullscreen></iframe>';
} else {
echo '<p>No video available for this article.</p>';
}
?>
</div>
Explanation:
<?php ... ?>: This is where we write PHP code.$videoUrl = GetFieldValue($arcRow, 'video_url');: This line calls the function, gets the value of thevideo_urlfield from the current article ($arcRow), and stores it in the$videoUrlvariable.if (!empty($videoUrl)): This is good practice. It checks if the field has a value before trying to display it, preventing empty<iframe>tags from showing up.echo '...': This prints the HTML for the video player if the URL exists.
Important Considerations & Best Practices
- Case Sensitivity: The
'field_name'parameter is case-sensitive. If your field is namedMyField, you must useGetFieldValue($arcRow, 'MyField'). Using'myfield'will return nothing. - Variable Name (
$arcRow): The name of the variable holding the article data can change depending on the context.- In article detail pages (
article_article.htm), it's almost always$arcRow. - In
arclistloops, it's usually$fieldsor$row. So you would useGetFieldValue($fields, 'field_name').
- In article detail pages (
- Check for Emptiness: Always check if a field has a value before using it to avoid errors or broken HTML.
- Performance:
GetFieldValueis very efficient. It only queries the database for the specific field you request, which is faster than retrieving all custom fields if you only need one.
GetFieldValue vs. GetFields
You might also see GetFields. It's important to know the difference:
| Function | What it Does | When to Use |
|---|---|---|
GetFieldValue |
Gets the value of one specific custom field. | When you only need a single field's value. It's more direct and efficient. |
GetFields |
Gets an array of ALL custom fields for an article. | When you need to display multiple custom fields and want to loop through them. |
Example of GetFields:
<?php
// Get all custom fields as an array
$allCustomFields = GetFields($arcRow);
// Now you can loop through them
if (!empty($allCustomFields)) {
echo '<h3>Additional Information:</h3>';
echo '<ul>';
foreach ($allCustomFields as $fieldName => $fieldValue) {
// You might want to exclude certain fields or format them
if ($fieldName != 'typeid' && !empty($fieldValue)) {
echo '<li><strong>' . $fieldName . ':</strong> ' . $fieldValue . '</li>';
}
}
echo '</ul>';
}
?>
In summary, GetFieldValue is your go-to function for cleanly and efficiently retrieving a single custom field's value in a DedeCMS template.
