dede field.keywords是什么?如何使用?

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

Of course! Let's break down dede:field.keywords in detail. This is a fundamental and very useful tag in the DedeCMS (织梦CMS) templating system.

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

What is dede:field.keywords?

In simple terms, dede:field.keywords is a DedeCMS template tag that retrieves and displays the keywords associated with a specific piece of content (an article).

These keywords are typically entered by the content author in the "Keywords" field when editing or publishing an article in the DedeCMS backend.


The Core Concept: Keywords in DedeCMS

To understand how to use the tag, you first need to understand where the data comes from.

  1. Input: When an admin or editor creates an article, they fill out a form. This form includes a field called "Keywords" (关键词).
  2. Storage: The keywords entered are stored in the dede_archives table, specifically in a field named keywords. This field is a text field where keywords are usually separated by commas (e.g., dedecms,模板,建站教程).
  3. Output: The dede:field.keywords tag reads this comma-separated string from the database and displays it on the front end.

How to Use dede:field.keywords

The basic syntax of the tag is very simple and is used within an article template file (e.g., article_article.htm).

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

Basic Display

This will simply print the raw keyword string as it is stored in the database.

{dede:field.keywords runphp='yes'}
    if(@me != '') @me = "<span class='keywords'>标签: " . @me . "</span>";
    else @me = '';
{/dede:field.keywords}

Explanation:

  • {dede:field.keywords}: The tag itself.
  • runphp='yes': This is a crucial attribute that tells DedeCMS to execute the PHP code inside the tag. Without this, @me would just be printed as text.
  • @me: This is a special PHP variable inside DedeCMS tags that holds the value of the field—in this case, the keyword string.
  • The if(@me != '') check ensures that nothing is displayed if the keywords field is empty. This prevents showing an ugly "标签: " with nothing after it.

Displaying Keywords as Clickable Links (Most Common Use Case)

The most frequent use for keywords is to turn them into links that lead to a search results page for that keyword. This is great for internal linking and SEO.

Here is the standard code to achieve this:

dede field.keywords
(图片来源网络,侵删)
{dede:field.keywords runphp='yes'}
    if(@me != ''){
        $kw = explode(',', @me);
        $tags = '';
        foreach($kw as $k){
            $k = trim($k);
            if($k != ''){
                // The 'keyword' parameter is what the search will look for
                $tags .= "<a href='/plus/search.php?keyword=".urlencode($k)."'>".$k."</a> ";
            }
        }
        @me = $tags;
    }else{
        @me = '';
    }
{/dede:field.keywords}

Step-by-Step Breakdown:

  1. if(@me != ''): Checks if there are any keywords.
  2. $kw = explode(',', @me);: Splits the comma-separated string (e.g., "dedecms,模板,教程") into an array (e.g., ['dedecms', '模板', '教程']).
  3. foreach($kw as $k): Loops through each keyword in the array.
  4. $k = trim($k);: Removes any accidental whitespace around the keyword.
  5. if($k != ''): A safety check to ensure we don't create a link for an empty keyword.
  6. $tags .= "<a href='...'>".$k."</a> ";: This is the core part.
    • It builds the link HTML.
    • /plus/search.php?keyword=: This is the standard DedeCMS search script. We are telling it to perform a search.
    • urlencode($k): This is a very important security and compatibility function. It ensures that special characters in keywords (like Chinese characters, spaces, or symbols) are correctly encoded for a URL.
    • We append each generated link to the $tags variable.
  7. @me = $tags;: Assigns the final string of links back to @me to be displayed on the page.

Displaying Keywords with a Limit

If you have many keywords and only want to show a few (e.g., the first 3), you can use PHP's array_slice() function.

{dede:field.keywords runphp='yes'}
    if(@me != ''){
        $kw = explode(',', @me);
        // Get only the first 3 keywords from the array
        $kw_slice = array_slice($kw, 0, 3);
        $tags = '';
        foreach($kw_slice as $k){
            $k = trim($k);
            if($k != ''){
                $tags .= "<a href='/plus/search.php?keyword=".urlencode($k)."'>".$k."</a> ";
            }
        }
        @me = $tags;
    }else{
        @me = '';
    }
{/dede:field.keywords}

Important Considerations & Best Practices

  1. Empty Keywords: Always check if @me is empty before trying to process it. This prevents errors and unwanted empty HTML elements on your page.
  2. SEO vs. User Experience:
    • Good: Keywords like "DedeCMS教程", "PHP学习", "网站建设" are descriptive and useful for users.
    • Bad: Stuffing keywords like "a, b, c, d, e, 1, 2, 3" is called "keyword stuffing" and is a black-hat SEO tactic that can get your site penalized by search engines. Train your content creators to use meaningful, relevant keywords.
  3. Location on the Page: Where you place this tag matters. Common places are:
    • Below the article title ({dede:field.title/}).
    • At the end of the article content.
    • In the article's sidebar metadata.
  4. Styling: The output is just plain HTML. You should style it with CSS to make it look good and distinguish it from the main content.

Example CSS:

.article-keywords {
    margin: 20px 0;
    padding: 10px;
    border: 1px dashed #ccc;
    font-size: 0.9em;
    color: #666;
}
.article-keywords a {
    color: #0066cc;
    text-decoration: none;
    margin-right: 8px;
}
.article-keywords a:hover {
    text-decoration: underline;
}

And in your HTML, you would wrap the tag with a div that has this class:

<div class="article-keywords">
    {dede:field.keywords runphp='yes'}
        // ... your PHP code from example 2 ...
    {/dede:field.keywords}
</div>
-- 展开阅读全文 --
头像
dede arclist标签row=10参数如何设置?
« 上一篇 12-07
织梦dedecms如何开启json api接口?
下一篇 » 12-07

相关文章

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

目录[+]