1

Under Sprout SEO 2 I am able to obtain the SEO metadata for a specific entry as follows:

{% do craft.sproutSeo.meta({
    id: entry.id,
    title: entry.title,
    description: entry.body|striptags|slice(0, 160)
}) %}
{{ craft.sproutSeo.getOptimizedMeta().basic.title }}
{{ craft.sproutSeo.getOptimizedMeta().basic.description }}

Here I am using the entry's title and body fields as fallback SEO metadata if none is specified on the entry. This is all documented for Sprout SEO 2.

How do I achieve this under Sprout SEO 3? The closest I can get is as follows:

{% do craft.sproutSeo.meta({
    id: entry.id
}) %}
{% sproutseo 'optimize' %}
{{ metadata.meta.search.title }}
{{ metadata.meta.search.description }}

Although this requires disabling automatic metadata rendering, which I require elsewhere, and enabling the custom metadata variable. It also omits the fallback SEO metadata as this now overrides the entry's metadata.

0

Sprout SEO 3 allows this same functionality but takes a different approach.

With Sprout SEO 3, in most cases, you can replace all of the template code that was required in Sprout SEO 2 with the single tag: {% sproutseo 'optimize' %} and the new Element Metadata field.

The Element Metadata field allows you to map the fields you wish to have as fallbacks. So, where you previously may have done this in Sprout SEO 2:

{% do craft.sproutSeo.meta({
    id: entry.id,
    title: entry.title,
    description: entry.body|striptags|slice(0, 160)
}) %}

You can now just set up an Element Metadata field and map the Title to the Entry Title and so on. The Element Metadata field supports one to one mappings or Custom Patterns where you can use twig code to define the metadata you wish to map.

If you wish to get more specific with the Element Metadata and have both the ability to do a fallback mapping and allow the user to override the metadata values, you can enable the Meta Details Fields in the Advanced settings area.

You may need to setup more than one Element Metadata field if you have more than one type of mapping you wish to do across various sections of your site.

The entry.id variable no longer needs to get used in your template in Sprout SEO 3. Sprout SEO 3 now identifies the entry that is being loaded on the page dynamically, just as Craft knows what entry variable to make available to the page.

I believe the above should address the fallback issue you describe and is the recommended approach to preparing your fallbacks in the most manageable and maintainable way. I've yet to have someone give me a scenario that doesn't fit in the above model, but if you feel like you have one, feel free to reach out to us via support and I'd be happy to chat it through.


To address your final comment:

Although this requires disabling automatic metadata rendering, which I require elsewhere, and enabling the custom metadata variable. It also omits the fallback SEO metadata as this now overrides the entry's metadata.

If it's really necessary to override the default metadata, Sprout SEO gives you control over two things:

  1. To make the prioritized metadata available to the page as a variable
  2. To disable the default rendering of the metadata

These two settings should give you control over a scenario where you want to both control how some metadata is output on your own and output some metadata in the same way that we do. For more advanced use cases, you just get to manage how the logic works around that on your own.

A very general example of how this could be done would be by making the metadata variable available to the page and copying over the default Sprout SEO metadata.html and wrapping a conditional around it. Something like:

{% if somethingIsTrue %}
    {# Do something special #}
{% else %}
    {# Output the default metadata template found in:
       sproutseo/templates/_special/metadata.html #}
{% endif %}
|improve this answer|||||
  • Thanks for the comprehensive answer. I have decided not to implement fallback metadata in order to simplify things. Regarding the use of entry.id, I probably wasn't explicit enough in stating that I was iterating over a list of entries, each of which I needed the metadata for. I've ended up using expressions such as {{ entry.sproutSeoElementMetadata.title }} for this. – Mark Hobson Apr 20 '17 at 10:41
  • Thanks for the clarification, Mark. We do have on our list to make it easier to output metadata for each Element in a loop. If you wish to talk things through further, feel free to reach out to us via support. We appreciate understanding these unique use cases as well as we can to make sure we're considering them as we continue development on the plugin. – Ben Parizek Apr 21 '17 at 22:14
1

I didn't understand Ben's answer, and I just needed to output an entry's SEO meta title with a fallback to the entry title in a for loop. Maybe this will help someone in a similar situation:

{% for entry in entries %}
    {% if entry.sproutSeoElementMetadata.title %}
        {% set title = entry.sproutSeoElementMetadata.title %}
    {% else %}
        {% set title = entry.title %}
    {% endif %}
    <li><a href="{{ entry.url }}">{{ title }}</a></li>
{% endfor %}

Just like entry.sproutSeoElementMetadata.title will grab the meta title of that entry, you can do the same with description: entry.sproutSeoElementMetadata.description

The meta image is a bit more complicated though:

{# get meta image's asset id #}
{% set imageId = entry.sproutSeoElementMetadata.ogImage %}

{# get the actual asset image by its id #}
{% set image = craft.assets.id(imageId).first %}

{# output the image's url #}
{{ image.url }}
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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