Skip to content

Instantly share code, notes, and snippets.

@davidensinger
Last active October 28, 2023 16:26
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save davidensinger/5431869 to your computer and use it in GitHub Desktop.
Save davidensinger/5431869 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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

@erlend-sh
Copy link

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
Copy link

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

@capturts
Copy link

capturts commented Jul 3, 2018

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

@Laogeodritt
Copy link

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