len参数如何设置标题长度?

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

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

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

What is titlelen?

In DedeCMS, titlelen is a label parameter used to control the length of the title text that is displayed when using various system labels. Its name is a combination of "title" and "length".

Its primary purpose is to prevent long titles from breaking your website's layout by truncating them to a specified number of characters.


How to Use titlelen

You will use titlelen inside DedeCMS template tags (labels). The syntax is simple:

{dede:标签名 titlelen='数字'}
    <a href="[field:arcurl/]">[field:title/]</a>
{/dede:标签名}
  • 标签名: The name of the DedeCMS label you are using (e.g., arclist, list, coolmenu).
  • titlelen='数字': The parameter where you specify the maximum number of characters for the title. For example, titlelen='30' will show the first 30 characters of the title.

Common Examples

Here are the most common places you'll use titlelen:

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

In {dede:arclist} (Article List)

This is the most frequent use case. You use arclist to display a list of articles on your homepage, in a sidebar, or in other blocks.

Without titlelen:

{dede:arclist typeid='1' row='5'}
    <li><a href="[field:arcurl/]">[field:title/]</a></li>
{/dede:arclist}

If an article title is "这是一个非常非常长的文章标题,可能会超出我们设定的宽度范围", it will stretch your layout.

With titlelen:

dede titlelen
(图片来源网络,侵删)
{dede:arclist typeid='1' row='5' titlelen='30'}
    <li><a href="[field:arcurl/]">[field:title/]</a></li>
{/dede:arclist}

Now, any title longer than 30 characters will be automatically shortened. For example, the title above might become: "这是一个非常非常长的文章标..."

In {dede:channel} (Channel List)

When listing栏目 (channels) or their子栏目 (sub-channels), you might want to limit the displayed name length.

{dede:channel type='top' row='8' titlelen='20'}
    <a href="[field:typelink/]">[field:typename/]</a>
{/dede:channel}

This will show the top 8 main channel names, but no longer than 20 characters each.

In {dede:myad} (Custom Ad)

While titlelen is for titles, it's worth noting that other labels like myad use similar parameters (e.g., textlen) for controlling ad text. The principle is the same.


Important Considerations and Best Practices

Character Counting: Chinese vs. English

A crucial point about titlelen is that it counts every character as one, regardless of whether it's a Chinese character, an English letter, or a number.

  • Chinese: 你好世界 (4 characters) -> titlelen='5' will show 你好世界.
  • English: Hello World (11 characters) -> titlelen='5' will show Hello.

This is generally what you want, but be aware of it when setting your limit.

Adding Suffixes (like )

It's standard practice to add an ellipsis () to indicate that the title has been cut off. You can achieve this with a simple PHP if statement within the label.

Example with Ellipsis:

{dede:arclist typeid='1' row='5' titlelen='30'}
    <li>
        <a href="[field:arcurl/]">
            [field:title function='(strlen(@me)>30 ? cn_substr(@me, 30)."..." : @me)'/]
        </a>
    </li>
{/dede:arclist}

Let's break down this function:

  • strlen(@me): Gets the full length of the original title.
  • strlen(@me)>30 ? ... : ...: This is a ternary operator. It checks if the length is greater than 30.
    • If true: It runs cn_substr(@me, 30)."...". cn_substr is a DedeCMS function that safely cuts a string to a specified length. It appends to the end.
    • If false: It just displays the original title @me as is.

SEO vs. Display

Be mindful that titlelen only affects what the user sees on your page. It does not change the actual <title> tag of the HTML page, which is critical for SEO.

  • Display Title: Controlled by titlelen in your template. Short, user-friendly.
  • SEO Title: Controlled by the "Title" field in the DedeCMS article backend. This should be a full, descriptive title for search engines.

The full SEO title is still used in the <title> tag of the article's own page, and in the link text that search engines index.


Summary

Feature Description
What it is A DedeCMS label parameter to limit the display length of a title.
Purpose To prevent layout breakage from long titles.
How to use Add titlelen='N' to your DedeCMS labels (e.g., arclist, channel).
Key Tip It counts every character as one (Chinese, English, etc.).
Best Practice Combine it with a PHP function like cn_substr to add an ellipsis () for better user experience.
SEO Note It only affects the visible title on the page, not the SEO title in the <title> tag.
-- 展开阅读全文 --
头像
织梦百度实时推送模块如何实现?
« 上一篇 今天
dede如何调用当前栏目下的文章?
下一篇 » 今天

相关文章

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

目录[+]