如何自定义 <title> 个 SilverStripe 页面

How do I customize <title> of SilverStripe pages

如何自定义 SilverStripe 页面的 <title>

现在是

<title><% if $MetaTitle %>$MetaTitle<% else %>$Title<% end_if %> &raquo; $SiteConfig.Title</title>

您可以在 CMS 的 "Title" 字段中设置页面标题。

如果您希望更改整个站点的标题,请在“站点配置”部分编辑标题(最多匹配 $SiteConfig.Title)。

一般来说,这些变量只是从 CMS 中填充,因此您可以随意使用其他变量自定义它们,或者根据需要在 CMS 中编辑它们的值。

您当前的页面模板页面 <title> 标签是:

<title>
    <% if $MetaTitle %>$MetaTitle<% else %>$Title<% end_if %> 
    &raquo; $SiteConfig.Title
</title>

您可以更改它以使用您想要的任何变量或内容。

您当前的模板代码检查页面是否定义了 $MetaTitle。如果是这样,它将使用它。否则它将使用页面 $Title.

最后一部分将站点标题 $SiteConfig.Title 添加到末尾。该字段可以在 CMS 的“设置”选项卡上找到。

MetaTitle 变量已从 3.1 的核心 SilverStripe 代码中删除。如果您想重新添加此功能,您可以通过安装 SilverStripe MetaTitle module 或自己将变量和输入添加到页面 class 来实现。

下面是将 MetaTitle 变量添加到 Page class 的一些代码:

class Page extends SiteTree {

    private static $db = array(
        'MetaTitle' => 'Varchar(255)'
    );

    public function getCMSFields() {
        $fields = parent::getCMSFields();

        $fields->addFieldToTab(
            'Root.Main',
            TextField::create('MetaTitle')
                ->setRightTitle('Shown at the top of the browser window and used as the "linked text" by search engines.')
                ->addExtraClass('help'),
            'MetaDescription'
        );

        return $fields;
    }
}

该变量将出现在 CMS 中每个页面的 Content 字段下方。