Yen
Yen

https://laiyenju.notion.site

Open Graph with Twitter Cards to realize the display of the website's community platform

My website articles have pictures!
Many people use Open Graph combined with Twitter Card, so that when their articles are shared on social platforms, they can show graphs. I also try to use this method to record the concepts learned.

write before you start

My blog site is built with Jekyll + GitHub. To add graphics, I need to manually add code in the default.html default template referenced by every page. The newly added code is metadata, so it should be written in the <head></head> block of default.html.

 # My Jekyll site structure
 -- _layouts #Web template folder
 -- default.html #file to add code to
 --page.html
 --post.html
 --tags.html
 -- _posts #document folder
 -- article1.md
 --article2.md
 -- images #image folder (display images and website logos are placed here)
 --favicon.png
 --feature_image1
 --feature_image2
 -- _assets
 --data
 -- _config.yml # Website configuration file, setting the display should refer to the settings of this file
 -- index.html # index.html is the home page, tags.md, about.md are the sub-pages of the website
 --tags.md
 -- about.md

The relationship between Twitter Cards and Open Graph

Twitter Cards

  • twitter:card (required)|The style of the twitter card, which needs to be set to summary_large_image to display the image.
  • twitter:site (required)|The twitter account of the blog or website. If you don't have one, you can write your own twitter account.
  • twitter:title (required)|Article title
  • twitter:description |Article summary
  • twitter:image |
  • twitter:creator | This parameter can be used if the article has more than 2 co-authors.

Open Graph

  • og:site_name |Site name
  • og:description| Abstract
  • og:image| Display image
  • og:url| Link to article page

From the list, we can see that twitter and Open Graph have overlapping functions of "article abstract" and "display image", so that we can integrate the two without writing too much metadata. In default.html, the logic of integrating the code is as follows:

  1. The document front matter of each article should set feature_image as the display image.
  1. Regardless of whether the jekyll template has page or post distinction, as long as it is a document, the parameter is page.
Please click the HTML tab to view

success! 👏👏👏

https://twitter.com/theYenLai/status/1247810860656910338?s=20

Several pits that you may accidentally fall into

Pit 1: Some articles do not have pictures, what should I use to display pictures?

Use {% if page.feature_image %} ..{% endif %} conditional expression to handle the display effect of images with and without images in the article. When the article does not set feature_image, the favicon of the website can be used to display the image. When setting up image links, pay special attention to the spelling. Since my feature_image and favicon are relative positions, the writing method is as follows

  • feature_image is written in the front matter of the manuscript: feature_image:images/image1
  • The favicon is written in _congif.yml: favicon: images/favicon.png

The feature_image is attached to the page, and the favicon is attached to the site, but the display image of the meta data must be a complete URL. Taking my website as an example, the display URL should be written as https://laiyenju.github.io/images/image1 , which is created by The combination of "site homepage location" and "display image location", so metadata will be written as <meta property="og:image" content="{{ site.url }}/{{ page.feature_image }}"> , the metadata of favicon is written in the same way.

 # Integrate into conditional {% if page.feature_image %}
  <meta property="og:image" content="{{ site.url }}/{{ page.feature_image }}">
  
  {% else %}
  <meta property="og:image" content="{{ site.url }}/{{ site.favicon }}">

  {% endif %}

Pit 2: twitter:site is different from og:site_name

This part is often confusing. At first glance, you may think that twitter:site and og:site_mame are both the fields of the website name, but only og:site_name can display the website name, and twitter:site is the official account of the website on twitter.


results of testing

Regarding Open Graph and Twitter Card to achieve the effect of sharing and displaying pictures on the webpage, because I mainly use Twitter as the sharing channel, I spend more effort in adjusting the visual presentation of Twitter Card. After testing, it is determined that the presentation effect of sharing on Twitter and Facebook is the best. But there is no guarantee that it will work on every platform. If you want the graphics effects of your blog articles to be generally applicable to other platforms, you need to know more about the details of the Open Graph protocol .

successful pipeline

  • Twitter, private messages and public posts all succeed
  • Facebook, private messages and public posts all successful
  • LINE

failed pipeline

  • Telegram, only the website name, article title and abstract, no graphics.
  • iMessage, only the article title and website logo.

References


This article was also published in my Tinker's Backchannel newsletter.

Tinker's Backchannel shares content for data analysis, visualization, news media, and interactive design. If you are also interested in these, I look forward to learning and communicating together.

Twitter GitHub Website

CC BY-NC-ND 2.0

Like my work?
Don't forget to support or like, so I know you are with me..

Loading...

Comment