Add Gravatar Images Next to Your Shopify Comments
Most Shopify themes don't have Gravatar support out of the box. Many commenters already have an avatar registered to their email, and it's great to display those to make your comments more personable and engaging. There's a reason every social network ever has avatars and user photos, right?
Thankfully Shopify supports the MD5 filter, which is needed to convert your email comments to strings before they can be sent to Gravatar, which makes this code very simple.
You'll want to open your theme's main-article.liquid file and then look for your comment section. It will look a little like the code below, just do a CTRL+F search for the word "comment":
{%- for comment in article.comments -%}
<article id="{{ comment.id }}" class="article-template__comments-comment">
{{ comment.content }}
<footer class="right">
<span class="circle-divider caption-with-letter-spacing">{{ comment.author }}</span>
<span class="caption-with-letter-spacing">
{{- comment.created_at | time_tag: format: 'date' -}}
</span>
</footer>
</article>
{%- endfor -%}
This is the magic line to add:
<img src="https://www.gravatar.com/avatar/{{ comment.email | md5 }}?s=80&d=robohash" alt="{{ comment.author }}'s Avatar" class="user-avatar">
So your final code might look something like this:
{%- for comment in article.comments -%}
<article id="{{ comment.id }}" class="article-template__comments-comment">
{{ comment.content }}
<footer class="right">
<!-- Newly added Gravatar here -->
<img src="https://www.gravatar.com/avatar/{{ comment.email | md5 }}?s=80&d=robohash" alt="{{ comment.author }}'s Avatar" class="user-avatar">
<span class="circle-divider caption-with-letter-spacing">{{ comment.author }}</span>
<span class="caption-with-letter-spacing">
{{- comment.created_at | time_tag: format: 'date' -}}
</span>
</footer>
</article>
{%- endfor -%}
That's it! Now your comments will load the Gravatar image associated with your commenter's email.
You might want to add a little styling to make these look nicer, like making the images rounded or floating them to the left. Example:
.user-avatar {
float:left;
display:block;
margin-right:30px;
margin-bottom:30px;
border-radius:50px;
}
Feel free to play with the styling as needed.
If this helped you or if you have any questions, please drop me a comment below!
Comments