Jump to:

3392 Posts in 976 Topics by 852 members

Template Questions

SilverStripe Forums » Template Questions » Changing default title and meta tags

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 6667 Views
  • DanStephenson
    Avatar
    Community Member
    115 Posts

    Changing default title and meta tags Link to this post

    Hi all,

    I know that a page's title can be changed in the admin panel, but then just that page's title, as entered shows up. What I was hoping for, was to create a title in this format:

    Site Name - $Title

    Can the title tag output using $MetaTags be customized at all? What about meta tags? I'd love to be able to enter a pre-filled list of keywords for every page, and then allow users to add to them after, same with the description.

  • Howard
    Avatar
    Community Member
    215 Posts

    Re: Changing default title and meta tags Link to this post

    Title: this is easily done with Silverstripe. In your Page.ss file change $Metatags to $Metatags(false) this excludes the Title from being added automagically, then you can include $Title in the head by going like:

    <title>$Title - Site Name</title>

    Keywords and Description: Again this is easy to edit, for each page that is created in the CMS there is a tab above the page-name editing box called "Meta-data". In this tab you can change the URL of the page and add keywords and edit the description

  • DanStephenson
    Avatar
    Community Member
    115 Posts

    Re: Changing default title and meta tags Link to this post

    Thanks Howard, that is excellent info about the title.

    I know about the tab for meta info, but is there a way I could pre-fill it with sitewide keywords for each new page, and then the author add their own to the list?

  • neilcreagh
    Avatar
    Community Member
    88 Posts

    Re: Changing default title and meta tags Link to this post

    Hi Guys,

    I've added default global Meta Tags by using the Homepage Meta Tags as the default set, then only replacing them if individual meta tags are actually specified for that page. So, if most pages have no meta tags set tehn the homepage set will be used as default but if one of the pages has a different title, or description or keywords it will use them.

    You can have the correct page title appear in the default set on each individual page. So, if a page has left any Meta tags blank it actually uses the default set (homepage tags) PLUS the actual page title. eg. About Us ›› The ABC Company Website

    To do this you need to add a simple function to your Page_Controller in Page.php:
       function GetHomePage(){
       return DataObject::get("HomePage");
       }

    Then, on the main Page.ss template call your Meta Tags like so:

    <title><% if MetaTitle %>$MetaTitle<% else %>$Title &raquo; <% control GetHomePage %>$MetaTitle<% end_control %><% end_if %> </title>

    <meta name="title" content="<% if MetaTitle %>$MetaTitle<% else %>$Title &raquo; <% control GetHomePage %>$MetaTitle<% end_control %><% end_if %>" />

    <meta name="description" http-equiv="description" content="<% if MetaDescription %>$MetaDescription<% else %>$Title <% control GetHomePage %>$MetaDescription<% end_control %><% end_if %>" />

    <meta name="keywords" http-equiv="keywords" content="<% if MetaKeywords %>$MetaKeywords<% else %>$Title <% control GetHomePage %>$MetaKeywords<% end_control %><% end_if %>" />

    Hope this helps!

    ps. By default Silverstripe populates the MetaTitle with the page title, so I've included the Default Title from the Homepage after it.

    6667 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.