欢迎光临
我们一直在努力

为wordpress站点添加og标签方法

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 姓氏
赞(0) 打赏
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 ivillcn@qq.com 举报,一经查实,本站将立刻删除。
文章名称:《为wordpress站点添加og标签方法》
文章链接:https://www.bilibiji.com/article/338.html

评论 抢沙发

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续给力更多优质内容,让我们一起创建更加美好的网络世界!

微信扫一扫

登录

找回密码

注册