How do I customize the <title>
of SilverStripe pages?
Right now it's
<title><% if $MetaTitle %>$MetaTitle<% else %>$Title<% end_if %> » $SiteConfig.Title</title>
How do I customize the <title>
of SilverStripe pages?
Right now it's
<title><% if $MetaTitle %>$MetaTitle<% else %>$Title<% end_if %> » $SiteConfig.Title</title>
Your current page template page <title>
tag is:
<title>
<% if $MetaTitle %>$MetaTitle<% else %>$Title<% end_if %>
» $SiteConfig.Title
</title>
You can change this to use any variable or content that you would like.
Your current template code checks if the page has a $MetaTitle
defined. If it does it will use this. Otherwise it will use the page $Title
.
The last part adds the site title $SiteConfig.Title
to the end. This field can be found in the CMS on the Settings tab.
The MetaTitle
variable was removed from the core SilverStripe code in 3.1. If you would like to add this functionality back in you can do this by installing the SilverStripe MetaTitle module or by adding the variable and input to your Page class yourself.
Here is some code to add the MetaTitle
variable to the 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;
}
}
The variable will appear in the CMS on each page underneath the Content
field.
You can set the page title in the CMS in the "Title" field.
If you wish to change the title of the entire site, edit the Title in the Site Configuration section (which matches up to $SiteConfig.Title
).
In general, those variables are just populated from the CMS, so feel free to customize them with other variables, or edit their values in the CMS as you need.