dede field title是什么?如何使用?

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

Of course! The dede:field tag is one of the most fundamental and powerful tags in the DedeCMS (织梦CMS) template engine. The dede:field name='title' tag specifically is used to display the title of the current content (article, page, etc.).

dede field title
(图片来源网络,侵删)

Let's break it down in detail.


Basic Usage: dede:field name='title'

This is the simplest way to display the title of an article on its detail page.

Template Code:

<h1>{dede:field name='title'/}</h1>

What it does: When an article page is loaded, this tag will be replaced by the actual title text stored in the database for that specific article.

dede field title
(图片来源网络,侵删)

Example Output: If the article's title in the DedeCMS backend is "深入理解DedeCMS的标签系统", the final HTML output will be:

<h1>深入理解DedeCMS的标签系统</h1>

Common Attributes for dede:field name='title'

The dede:field tag supports several useful attributes to modify its output.

a) function Attribute

This is the most powerful attribute. It allows you to apply a PHP function to the field's value before displaying it. This is extremely useful for formatting.

Common function Examples:

dede field title
(图片来源网络,侵删)
  • HTML Entity Encoding (for security): To prevent XSS attacks, it's good practice to escape HTML characters in titles.

    {dede:field name='title' function='htmlspecialchars(@me)'/}

    If the title was <script>alert('hack')</script>, this would safely display it as text instead of executing the script.

  • String Trimming (for display length): To limit the title to a certain number of characters, especially useful in lists or meta descriptions.

    {dede:field name='title' function='cn_substr(@me, 30)'/}

    This will display the first 30 Chinese characters of the title.

  • Custom Function: You can create your own PHP functions in include/helpers/extend.helper.php and use them here.

    {dede:field name='title' function='my_custom_title_format(@me)'/}

b) runphp Attribute

This attribute is an alternative to function and gives you more control. When runphp='yes', the value of the tag is treated as PHP code. You must echo the final output.

Example: Conditional Title Formatting Let's say you want to add a prefix "[New]" to articles published within the last 7 days.

{dede:field name='title' runphp='yes'}
    $pubdate = @me; // In a real scenario, you'd get this from another field
    $sevenDaysAgo = time() - (7 * 24 * 60 * 60);
    // Assuming you have the article's publish date available, e.g., from {dede:field.pubdate}
    // For this example, let's pretend @me is the timestamp
    if (@me > $sevenDaysAgo) {
        @me = "[New] " . @me;
    }
    echo @me;
{/dede:field}

Note: The runphp example is simplified. A more practical use would involve combining it with other fields.

c) style and class Attributes

These are standard HTML attributes that you can add directly to the tag for styling.

{dede:field name='title' style='color: #ff6600; font-weight: bold;' class='article-title'/}

This will render the title with inline CSS and a CSS class.


Practical Use Cases

Use Case 1: Displaying the Title in the Browser Tab (<title>)

This is a critical use case for SEO. The <title> tag of an HTML page should contain the article's title.

Template Code (in article_article.htm):

<head>
    <meta charset="UTF-8">{dede:field name='title'/} - {dede:global.cfg_webname/}</title>
    <!-- other meta tags and CSS links -->
</head>

Output:深入理解DedeCMS的标签系统 - 我的织梦网站`

Use Case 2: Using a Short Title or SEO Title

Sometimes you want a different title for display on the page versus what's in the main <title> tag (e.g., for social media shares or a more compact display). DedeCMS often uses a shorttitle field for this.

Template Code:

<!-- Main H1 Tag (Full Title) -->
<h1>{dede:field name='title'/}</h1>
<!-- Social Media / Summary Title (Short Title) -->
<div class="social-share">
    Share: "{dede:field name='shorttitle'/}"
</div>

If the shorttitle is empty, it will display nothing. You can combine this with function to provide a fallback.

Use Case 3: Creating a Breadcrumb Navigation

Breadcrumbs help users understand where they are on the site. The title is a key part of this.

Template Code:

<nav aria-label="breadcrumb">
    <ol class="breadcrumb">
        <li><a href="{dede:global.cfg_cmsurl/}/">首页</a></li>
        <li><a href="/category/">{dede:type typeid=''}[field:typename/]{/dede:type}</a></li>
        <li class="active">{dede:field name='title'/}</li>
    </ol>
</nav>

Important Context: Where dede:field name='title' Works

  • Article Detail Pages (article_article.htm): This is its primary and most common use.
  • *Category Pages (`category_.htm):** Here,title` refers to the name of the category, not an article title.
  • Other Content Pages: It will work for any content type that has a "title" field, such as pages (page_*.htm).

Troubleshooting

  • Problem: The tag is not working and is showing as {dede:field name='title'/} in the final HTML.

    • Cause 1 (Most Common): You are on a page that doesn't have a single article context (e.g., the homepage, a search results page). dede:field tags are for displaying fields of a specific piece of content. On these pages, you should use dede:arclist or dede:list to loop through articles and use arctitle or title inside the loop.
    • Cause 2: The template file is not the correct one for the content type you're viewing.
    • Cause 3: There might be a syntax error in your template, or the DedeCMS cache needs to be cleared. Try clearing the cache in the DedeCMS backend.
  • Problem: The title is not being formatted correctly with the function attribute.

    • Cause: The PHP function name might be misspelled, or the function might not exist. Double-check the function name and ensure it's a valid PHP function. The @me variable represents the original value of the field.

Summary

Tag Purpose Example
{dede:field name='title'/} Basic Display: Outputs the raw title text. <h1>{dede:field name='title'/}</h1>
{dede:field name='title' function='...'/} Formatted Display: Applies a PHP function to the title. {dede:field name='title' function='cn_substr(@me, 50)'/}
{dede:field name='title' runphp='yes'}...{/dede:field} Advanced Logic: Executes custom PHP code to manipulate the title. See conditional example above.
{dede:field name='shorttitle'/} Alternative Title: Uses a secondary, often shorter, title field. <meta property="og:title" content="{dede:field name='shorttitle'/}" />

Understanding dede:field is key to mastering DedeCMS templating. The name='title' variant is your go-to for displaying the main title of any content item.

-- 展开阅读全文 --
头像
dede trim函数如何正确使用及去除空格?
« 上一篇 2025-12-19
C语言宏定义1001例,如何高效掌握核心技巧?
下一篇 » 2025-12-19

相关文章

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

目录[+]