A-
A+
WordPress文章页面自动输出description和Keywords以及添加og社交标签的简单代码
最近看到很多人的站都带og标签,经过搜索资料知道,og标签是强化社交分享的,所以我也要添加上og标签
在WordPress里可以用插件添加,也可以自己添加。
用插件的话,就用安装了 Yoast/RankMath 等SEO插件就可以了。
代码的话,添加到functions.php里
/**
* 自定义 TDK + OpenGraph OG 社交标签
* 挂载至 wp_head 自动输出,兼容 PHP7.4 WordPress
*/
function custom_seo_tdk_og_output() {
$paged = get_query_var('paged') ? (int)get_query_var('paged') : 1;
$page_suffix = $paged > 1 ? ' - 第' . $paged . '页' : '';
$site_name = get_bloginfo('name');
global $post;
// OG全局控制变量
$og_type = 'website';
$og_img = '';
// 替换为你的网站完整HTTPS默认分享图
$default_og_img = 'https://你的域名/wp-content/themes/Ality/img/logo1.png';
// ========== 标题 Title ==========
if ( is_home() ) {
echo '<title>' . esc_html(get_bloginfo('name')) . ' - ' . esc_html(get_bloginfo('description')) . '</title>' . "\n";
} elseif ( is_search() ) {
echo '<title>搜索结果' . esc_html($page_suffix) . ' - ' . esc_html($site_name) . '</title>' . "\n";
} elseif ( is_single() ) {
$og_type = 'article';
$title = trim(wp_title('', false));
$single_page = get_query_var('page') ? ' - 第' . get_query_var('page') . '页' : '';
echo '<title>' . esc_html($title . $single_page) . ' - ' . esc_html($site_name) . '</title>' . "\n";
} elseif ( is_page() ) {
$title = trim(wp_title('', false));
echo '<title>' . esc_html($title) . ' - ' . esc_html($site_name) . '</title>' . "\n";
} elseif ( is_category() ) {
$cat_title = single_cat_title('', false);
echo '<title>' . esc_html($cat_title . $page_suffix) . ' - ' . esc_html($site_name) . '</title>' . "\n";
} elseif ( is_year() ) {
echo '<title>' . esc_html(get_the_date('Y年') . '所有文章' . $page_suffix) . ' - ' . esc_html($site_name) . '</title>' . "\n";
} elseif ( is_month() ) {
echo '<title>' . esc_html(get_the_date('Y年n月') . '所有文章' . $page_suffix) . ' - ' . esc_html($site_name) . '</title>' . "\n";
} elseif ( is_day() ) {
echo '<title>' . esc_html(get_the_date('Y年n月j日') . '所有文章' . $page_suffix) . ' - ' . esc_html($site_name) . '</title>' . "\n";
} elseif ( is_404() ) {
echo '<title>页面不存在 - ' . esc_html($site_name) . '</title>' . "\n";
} elseif ( is_tag() ) {
$tag_title = single_tag_title('', false) . ' 相关文章';
echo '<title>' . esc_html($tag_title . $page_suffix) . ' - ' . esc_html($site_name) . '</title>' . "\n";
} elseif ( is_tax('notice') ) {
$tax_title = single_term_title('', false);
echo '<title>' . esc_html($tax_title . $page_suffix) . ' - ' . esc_html($site_name) . '</title>' . "\n";
}
// ========== 文章描述、关键词 ==========
$description = '';
$keywords = '';
if ( is_single() ) {
$post_desc = get_post_meta($post->ID, 'description', true);
$post_keys = get_post_meta($post->ID, 'keywords', true);
if ( ! empty($post_desc) ) {
$tmp_desc = trim($post_desc);
$tmp_desc = preg_replace('/\s+/', ' ', $tmp_desc);
$description = $tmp_desc;
} elseif ( ! empty($post->post_excerpt) ) {
$tmp_desc = trim($post->post_excerpt);
$tmp_desc = preg_replace('/\s+/', ' ', $tmp_desc);
$description = $tmp_desc;
} else {
$content = wp_strip_all_tags($post->post_content);
$content = trim($content);
$content = preg_replace('/\s+/', ' ', $content);
if (function_exists('mb_substr')) {
$description = mb_substr($content, 0, 120, 'utf-8');
} else {
$description = substr($content, 0, 120);
}
}
if ( ! empty($post_keys) ) {
$keywords = $post_keys;
} else {
$tags_arr = wp_get_post_tags($post->ID);
if ( ! empty($tags_arr) ) {
$tag_names = array();
foreach ($tags_arr as $tag) {
$tag_names[] = $tag->name;
}
$keywords = implode(',', $tag_names);
}
}
}
// ========== Meta 标签输出 ==========
if ( is_single() && ! empty($description) ) {
echo '<meta name="description" content="' . esc_attr($description) . '" />' . "\n";
if ( ! empty($keywords) ) {
echo '<meta name="keywords" content="' . esc_attr($keywords) . '" />' . "\n";
}
}
if ( is_page() ) {
$page_desc = get_post_meta($post->ID, 'description', true);
$page_keys = get_post_meta($post->ID, 'keywords', true);
if ( ! empty($page_desc) ) {
$page_desc = trim($page_desc);
$page_desc = preg_replace('/\s+/', ' ', $page_desc);
echo '<meta name="description" content="' . esc_attr($page_desc) . '" />' . "\n";
}
if ( ! empty($page_keys) ) {
echo '<meta name="keywords" content="' . esc_attr($page_keys) . '" />' . "\n";
}
}
if ( is_category() ) {
$cat_desc = category_description();
if ( ! empty($cat_desc) ) {
echo '<meta name="description" content="' . esc_attr($cat_desc) . '" />' . "\n";
}
}
if ( is_tax('notice') ) {
$tax_desc = term_description();
if ( ! empty($tax_desc) ) {
echo '<meta name="description" content="' . esc_attr($tax_desc) . '" />' . "\n";
}
$tax_keys = get_term_meta(get_queried_object_id(), 'keywords', true);
if ( ! empty($tax_keys) ) {
echo '<meta name="keywords" content="' . esc_attr($tax_keys) . '" />' . "\n";
}
}
if ( is_tag() ) {
$tag_name = single_tag_title('', false);
$tag_desc = $tag_name . ' 相关教程、技术文章合集,分享实用技巧与解决方案。';
echo '<meta name="description" content="' . esc_attr($tag_desc) . '" />' . "\n";
}
if ( is_home() ) {
$home_desc = get_option('cx_description');
$home_keys = get_option('cx_keywords');
if ( ! empty($home_desc) ) {
echo '<meta name="description" content="' . esc_attr($home_desc) . '" />' . "\n";
}
if ( ! empty($home_keys) ) {
echo '<meta name="keywords" content="' . esc_attr($home_keys) . '" />' . "\n";
}
}
if ( is_search() ) {
echo '<meta name="description" content="网站搜索结果页面,查找各类技术教程、电脑运维相关内容。" />' . "\n";
echo '<meta name="robots" content="noindex,nofollow" />' . "\n";
}
// ========== Canonical 规范链接 ==========
$canonical_url = '';
if ( is_home() ) {
$canonical_url = home_url('/');
} elseif ( is_singular() ) {
$canonical_url = get_permalink();
} elseif ( is_category() || is_tag() || is_tax() ) {
$term = get_queried_object();
$link = get_term_link($term);
if (!is_wp_error($link)) {
$canonical_url = $link;
}
} elseif ( is_archive() ) {
$canonical_url = get_archive_link();
}
if ( ! empty($canonical_url) ) {
echo '<link rel="canonical" href="' . esc_url($canonical_url) . '" />' . "\n";
}
// ========== OG标题组装 ==========
$og_title = '';
if (is_home()) {
$og_title = get_bloginfo('name') . ' - ' . get_bloginfo('description');
} elseif (is_search()) {
$og_title = '搜索结果' . $page_suffix . ' - ' . $site_name;
} elseif (is_single()) {
$t = trim(wp_title('', false));
$pg = get_query_var('page') ? ' - 第' . get_query_var('page') . '页' : '';
$og_title = $t . $pg . ' - ' . $site_name;
} elseif (is_page()) {
$og_title = trim(wp_title('', false)) . ' - ' . $site_name;
} elseif (is_category()) {
$og_title = single_cat_title('', false) . $page_suffix . ' - ' . $site_name;
} elseif (is_year()) {
$og_title = get_the_date('Y年') . '所有文章' . $page_suffix . ' - ' . $site_name;
} elseif (is_month()) {
$og_title = get_the_date('Y年n月') . '所有文章' . $page_suffix . ' - ' . $site_name;
} elseif (is_day()) {
$og_title = get_the_date('Y年n月j日') . '所有文章' . $page_suffix . ' - ' . $site_name;
} elseif (is_404()) {
$og_title = '页面不存在 - ' . $site_name;
} elseif (is_tag()) {
$og_title = single_tag_title('', false) . ' 相关文章' . $page_suffix . ' - ' . $site_name;
} elseif (is_tax('notice')) {
$og_title = single_term_title('', false) . $page_suffix . ' - ' . $site_name;
}
// OG图片逻辑
if ((is_single() || is_page()) && !empty($post)) {
if (has_post_thumbnail($post->ID)) {
$thumb_src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'full');
$og_img = $thumb_src[0];
} else {
preg_match_all('/<img.+?src=["\'](.+?)["\']/i', $post->post_content, $img_matches);
if (!empty($img_matches[1][0])) {
$og_img = $img_matches[1][0];
}
}
}
if (empty($og_img)) {
$og_img = $default_og_img;
}
// 输出基础OG标签
echo '<meta property="og:type" content="' . esc_attr($og_type) . '" />' . "\n";
echo '<meta property="og:site_name" content="' . esc_attr($site_name) . '" />' . "\n";
echo '<meta property="og:locale" content="zh_CN" />' . "\n";
if (!empty($og_title)) {
echo '<meta property="og:title" content="' . esc_attr(wp_strip_all_tags($og_title)) . '" />' . "\n";
}
if (!empty($description)) {
echo '<meta property="og:description" content="' . esc_attr($description) . '" />' . "\n";
}
if (!empty($canonical_url)) {
echo '<meta property="og:url" content="' . esc_url($canonical_url) . '" />' . "\n";
}
echo '<meta property="og:image" content="' . esc_url($og_img) . '" />' . "\n";
// 文章专属OG扩展标签
if ($og_type === 'article' && is_single()) {
$publishing = get_the_date('c');
$modified = get_the_modified_date('c');
$author_name = get_the_author_meta('display_name');
echo '<meta property="article:published_time" content="' . esc_attr($publishing) . '" />' . "\n";
echo '<meta property="article:modified_time" content="' . esc_attr($modified) . '" />' . "\n";
echo '<meta property="article:author" content="' . esc_attr($author_name) . '" />' . "\n";
$cats = get_the_category();
if (!empty($cats)) {
echo '<meta property="article:section" content="' . esc_attr($cats[0]->name) . '" />' . "\n";
}
$tags = get_the_tags();
if ($tags) {
foreach ($tags as $tag) {
echo '<meta property="article:tag" content="' . esc_attr($tag->name) . '" />' . "\n";
}
}
}
}
add_action('wp_head', 'custom_seo_tdk_og_output');
优势说明
1.不修改模板 header,切换主题不用重新改代码;
2.函数隔离变量,无全局变量污染,PHP 报错更少;
3.依靠 WP 原生 wp_head 钩子,输出位置标准,不会打乱页面结构;
4.完整保留你原有所有逻辑:自定义 description/keywords、自定义分类 notice、分页后缀、canonical、动态 og:type、图片优先级、article 系列标签;
5.兼容 PHP7.4、WordPress 标准规范。
补充注意
若你安装了 Yoast/RankMath 等 SEO 插件,插件自带 OG/TDK 会和本代码冲突,二选一使用;
functions.php 底部不要多出多余 闭合标签,避免空白行导致网站报错;
测试分享卡片可用微信链接预览工具、Facebook 分享调试工具抓取页面验证图片与标题展示。

所以我偷懒用了 Typecho, 自带这些个功能, 不想折腾哈哈