下载帮

您现在的位置是:首页 > CMS教程 > WordPress

WordPress

为WordPress博客文章页添加OG协议标签

2020-03-11 01:37WordPress

我们已经知道开放内容(OG)协议对SEO优化的作用,而在日常应用中,我们也能经常看到一些WordPress博客会在自己的页面的部分加入OG协议,实现起来很容易。

今天余斗就说说如何为WordPress博客文章页添加OG协议标签。因为WordPress大部分为文章形式页面,所以我们只需要添加以下几个协议即可:


	
og:type //页面内容类型,wordpress博客通常使用article文章类型即可。

og:title //这里是文章标题

og:description //页面的简单描述,wordpress中可以使用文章摘要

og:site_name //页面所在网站名,wordpress中使用博客网站名称

og:url //文章页面url地址

og:image //文章略缩图地址
 

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


	
<meta property="og:type" content="acticle">
<meta property="og:title" content="<?php wp_title('-', true, 'right'); echo get_option('blogname'); if (is_home ()) echo get_option('blogdescription'); if ($paged > 1) echo '-Page ', $paged; ?>">
<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:image" content="<?php wpjam_post_thumbnail();?>">
 

其中og:image我调用的自己主题的缩略图自定义函数,诸位如果要实现,需要自定义为自己的wordpress函数调用,参考文章《WordPress博客文章随机添加图片并设为缩略图》。

由于og协议目前只适用于wordpress文章页,只允许og协议出现在wordpress文章页,这里我们需要加一个判断:


	
<?php if (is_single()) { ?>
<meta property="og:type" content="acticle">
<meta property="og:title" content="<?php wp_title('-', true, 'right'); echo get_option('blogname'); if (is_home ()) echo get_option('blogdescription'); if ($paged > 1) echo '-Page ', $paged; ?>">
<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:image" content="<?php wpjam_post_thumbnail();?>">
<?php } ?>
 

这里就能实现WordPress博客的OG协议添加了,测试一下,看看效果:

文章评论