Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Adding Open Graph Tags to Jekyll
<meta content="{{ site.title }}" property="og:site_name">
{% if page.title %}
<meta content="{{ page.title }}" property="og:title">
{% else %}
<meta content="{{ site.title }}" property="og:title">
{% endif %}
{% if page.title %}
<meta content="article" property="og:type">
{% else %}
<meta content="website" property="og:type">
{% endif %}
{% if page.description %}
<meta content="{{ page.description }}" property="og:description">
{% else %}
<meta content="{{ site.description }}" property="og:description">
{% endif %}
{% if page.url %}
<meta content="{{ site.url }}{{ page.url }}" property="og:url">
{% endif %}
{% if page.date %}
<meta content="{{ page.date | date_to_xmlschema }}" property="article:published_time">
<meta content="{{ site.url }}/about/" property="article:author">
{% endif %}
{% if page.image %}
<meta content="{{ site.url }}/assets/img/posts/{{ page.image }}" property="og:image">
{% else %}
<meta content="{{ site.url }}/assets/img/logo-high-resolution.png" property="og:image">
{% endif %}
{% if page.categories %}
{% for category in page.categories limit:1 %}
<meta content="{{ category }}" property="article:section">
{% endfor %}
{% endif %}
{% if page.tags %}
{% for tag in page.tags %}
<meta content="{{ tag }}" property="article:tag">
{% endfor %}
{% endif %}
@davidensinger

This comment has been minimized.

Copy link
Owner Author

@davidensinger davidensinger commented Apr 22, 2013

For more information, please see my post on Adding Open Graph Tags to Jekyll.

@erlend-sh

This comment has been minimized.

Copy link

@erlend-sh erlend-sh commented Nov 6, 2015

Couldn't this fit in nicely with Jekyll core? Any particular reason why you haven't made a pull request? Would be very nice for places like talk.jekyllrb.com as well, since then the Discourse forum would be able to OneBox Jekyll news posts automatically.

@Sparker0i

This comment has been minimized.

Copy link

@Sparker0i Sparker0i commented Jan 7, 2017

Can we modify this to support a post instead of a page?

@capturts

This comment has been minimized.

Copy link

@capturts capturts commented Jul 3, 2018

For some of these fields, should they use | escape?

@Laogeodritt

This comment has been minimized.

Copy link

@Laogeodritt Laogeodritt commented Jul 23, 2018

Line 18:

   <meta content="{{ site.url }}{{ page.url }}" property="og:url">

Jekyll sites can be hosted at a path deeper than the domain root (e.g. GitHub Project Pages are at <username>.github.io/<projectname>), which is usually indicated with site.baseurl. For maximum generality, I would recommend, for Jekyll 3.3 and later, that this line be changed to:

   <meta content="{{ page.url | absolute_url }}" property="og:url">

and for earlier versions of Jekyll that don't support the absolute_url filter,

   <meta content="{{ site.url | append: site.baseurl | append: page.url }}" property="og:url">
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment