3

I'd really like some help with Craft CMS, that's probably a very basic thing to achieve, but I've so far got no idea how to. In another post about SEO, I saw something like this written:

<meta name="title" content="{% if title is defined %}{{ title }}{% endif %}">
<meta name="description" content="{% if description is defined %}{{ description }}{% endif %}">

The first part, regarding the title, is in the _layout.html by default. And, in the CMS, above each entry's body, I have managed to display a box for the user to enter a title.

BUT, were I to add the second part, regarding the description, I have so far been unable to find out how in the CMS, I can add a box for the user to enter a description.

Thanks in advance for any help that anyone can provide.

With kind regards,

Mark

1 Answer 1

5

The Title field will be a part of every entry 90% of the time ('Singles' don't need a Title).

To add a custom field in Craft: Settings > Fields > (create a field)

To add the new field to a content type: Settings > Sections > (click the "Edit content-type" link to the right) > (drag your new field into the blue box) > hit Save.


I like to create a separate custom field for each e.g. "Meta Title" and "Meta Description" - I then add these to a separate tab within a given content-type. (see attached)

SEO fields

Normally, I leave the SEO fields optional.

Then, in _layout.html:

<title>{% block entryMetaTitle %}{% endblock %}{{ siteName }}</title>
<meta name="description" content="{% block entryMetaDescription %}{% endblock %}">

The block tags inherit content from an individual template e.g. domain.com/news/news-article-slug, will make use of a single-entry template e.g. craft/templates/news/_entry.html which will contain:

{% extends "_layouts/_layout" %}

{% block entryMetaTitle %}{% if entry.metaTitle %}{{ entry.metaTitle}} | {% elseif entry.title %}{{ entry.title}} | {% endif %} News | {% endblock %}
{% block entryMetaDescription %}{% if entry.metaDescription %}{{ entry.metaDescription}}{% else %}{{entry.summaryText}}{% endif %}{% endblock %}

{% block content %}
... 
{% endblock %}

The above will display any content which the client decides to add to the custom fields, OR if nothing has been added the template will display the entry Title + Summary Text (in this case)

This post might also help you to define your own approach: How do you handle SEO on your site?

3
  • Thanks for the quick answer. Apologies for taking so long to come back. Been away. I'm guessing the contents of the entryMetaDescription block also work if it was just placed into _layout.html? So, for example: <title>{% if entry.metaTitle %}{{ entry.metaTitle }} | {% elseif entry.title %}{{ entry.title }} | {% endif %}{{ siteName }}</title>
    – SYPOMark
    Commented Feb 17, 2016 at 8:55
  • It would, however I prefer having it in individual templates as it allows a little more control in cases where the the custom-field names might differ or not exist at all for a given content-type.
    – Matt P
    Commented Feb 18, 2016 at 10:29
  • 1
    ... and, additionally, if there's multiple channels, you might want to display the name of that channel as part of the title, something that wouldn't be so easily achieved with my last comment ...
    – SYPOMark
    Commented Feb 18, 2016 at 13:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.