Typecho: Template Code That Shows a Thumbnail and Summary When a Post Is Shared to Chat or Moments

When someone shares a post with a friend or to Moments, you want a thumbnail and a short summary to show up.

Using the Open Graph protocol, edit your theme's header.php and add the following inside the <head> tag (custom fields supported):

<?php if ($this->is('post') || $this->is('page')): ?>
<?php
// Grab the thumbnail
$thumb = '';

// Try the custom field first
if ($this->fields->thumb) {
    $thumb = $this->fields->thumb;
} 
// Otherwise, pull the first image out of the content
else {
    preg_match('/<img[^>]+src=[\'"]([^\'"]+)[\'"][^>]*>/i', $this->content, $matches);
    if (!empty($matches[1])) {
        $thumb = $matches[1];
    }
}

// Normalize the image path into a full URL
if (!empty($thumb)) {
    // Already a full URL (http/https or //)? Leave it as is
    if (!preg_match('/^(https?:)?\/\//i', $thumb)) {
        // Not a full URL -- prepend the site root URL
        if (strpos($thumb, '/') === 0) {
            // Starts with a slash, so it's an absolute path under the site root
            $thumb = rtrim($this->options->siteUrl, '/') . $thumb;
        } else {
            // Relative path: build it on the site root URL
            $thumb = rtrim($this->options->siteUrl, '/') . '/' . ltrim($thumb, '/');
        }
    }
} 
// No image? Fall back to the default
else {
    $thumb = rtrim($this->options->siteUrl, '/') . '/usr/themes/' . $this->options->theme . '/images/default-og.jpg';
}

// Build the description
if ($this->fields->excerpt) {
    $description = $this->fields->excerpt;
} elseif ($this->description) {
    $description = $this->description;
} else {
    $description = Typecho_Common::subStr(strip_tags($this->excerpt), 0, 150, '...');
}
?>
    <meta property="og:title" content="<?php $this->title() ?>" />
    <meta property="og:type" content="article" />
    <meta property="og:url" content="<?php $this->permalink() ?>" />
    <meta property="og:image" content="<?php echo $thumb ?>" />
    <meta property="og:image:width" content="1200" />
    <meta property="og:image:height" content="630" />
    <meta property="og:description" content="<?php echo htmlspecialchars($description) ?>" />
    <meta property="og:site_name" content="<?php $this->options->title() ?>" />

    <!-- Twitter Card -->
    <meta name="twitter:card" content="summary_large_image" />
    <meta name="twitter:title" content="<?php $this->title() ?>" />
    <meta name="twitter:description" content="<?php echo htmlspecialchars($description) ?>" />
    <meta name="twitter:image" content="<?php echo $thumb ?>" />

    <!-- WeChat-specific tags -->
    <meta property="wechat:article:title" content="<?php $this->title() ?>" />
    <meta property="wechat:article:description" content="<?php echo htmlspecialchars(Typecho_Common::subStr(strip_tags($description), 0, 120, '...')) ?>" />
    <meta property="wechat:article:image" content="<?php echo $thumb ?>" />
<?php endif; ?>

With this in place, shares of your Typecho blog on WeChat, QQ, Weibo, and similar platforms will show a clean thumbnail and summary.

Notes on how the image URL is resolved:

  1. A regex checks whether the value is already a full URL (starting with http://, https://, or //).
  2. If not, the code decides whether it's an absolute path (starting with /) or a relative one.
  3. Absolute paths are joined straight onto the site root URL.
  4. Relative paths are handled case by case (the simple version just prefixes the site root URL).

However the image path is stored, you end up with a complete URL, so thumbnails render properly when the post is shared on WeChat, QQ, and elsewhere.

There are other routes to the same result, of course, such as a plugin.

Leave a Comment

Comments No Comments

Table of Contents