为wordpress站点添加og标签方法

微信扫一扫,分享到朋友圈

为wordpress站点添加og标签方法
收藏 00

OG标签全程Open Graph Protocol,如果你不了解,推荐阅读:什么是og标签?HTML前缀属性「Open Graph Protocol」

因为WordPress大部分为文章形式页面,所以我们只需要添加以下几个协议即可:

og:type //页面内容类型,wordpress博客通常使用article文章类型即可。
og:title //这里是文章标题
og:description //页面的简单描述,wordpress中可以使用文章摘要
og:site_name //页面所在网站名,wordpress中使用博客网站名称
og:url //文章页面url地址
og:image //文章略缩图地址

添加方式为,找到当前主题使用header.php文件,在标签内部插入代码:

<?php if (is_single()) { ?>
<meta property="og:locale" content="zh_CN" />
<meta property="og:type" content="acticle">
<meta property="og:title" content="<?php the_title();?>">
<meta property="og:site_name " content="<?=get_bloginfo('name');?>">
<meta property="og:description" content="<?php the_excerpt();?>">
<meta property="og:url" content="<?php the_permalink();?>"/>
<meta property="og:author" content="<?php the_author_meta('display_name', $post->post_author); ?>" /> 
<meta property="og:release_date" content="<?php the_time('Y年m月d日 h:i');?>" />
<?php } ?>

需要注意的是在为 WordPress 主题添加meta property标签时,其中有一项是:

<meta property="og:author" content="作者" />

网上常见的方法是,在我测试下是不可用的

<meta property="og:author" content="<?php the_author(); ?>" />

其中

<?php the_author(); ?>

在这里使用不能显示作者名称,不知道帖代码者是怎么显示作者名称的,可能都是转载的。

解决方法:用以下代码替换

<?php the_author_meta('user_nicename', $post->post_author); ?>

其中默认user_nicename 是显示作者的登录名称,暴露重要信息有些危险。可以用下面的替换一下:

  • display_name 公开显示名称
  • user_nicename 昵称
  • first_name 名字
  • last_name 姓氏
一个热爱互联网的咸鱼
上一篇

什么是og标签?HTML前缀属性「Open Graph Protocol」

下一篇

在WordPress中压缩前端HTML代码方法汇总

你也可能喜欢

发表评论

您的电子邮件地址不会被公开。 必填项已用 * 标注

提示:点击验证后方可评论!

插入图片

热门

    抱歉,30天内未发布文章!
返回顶部