快轉到主要內容

Wordpress主题添加ogp协议meta标签

459 字1 分鐘00
作者Y Cheung

OGP(The Open Graph protocol)协议是一种新的HTTP头部标记,即Open Graph Protocol,即这种协议可以让网页成为一个“富媒体对象”。由于现在社交类网站大行其道,在HTML头部添加引用此类协议标注有助于被社交网站识别。

OG协议META标签

  • og:title 标题
  • og:type 类型
  • og:image 略缩图地址
  • og:url 页面地址
  • og:description 页面的简单描述
  • og:site_name 页面所在网站名
  • og:videosrc 视频或者Flash地址
  • og:audiosrc 音频地址

一般的,可以在wordpress文件header.php中<?php wp_head(); ?>前加入以下内容

 1<meta property="og:title" content="<?php the_title();?>" />
 2<meta property="og:site_name " content="<?php bloginfo('name'); ?>" />
 3<?php if (is_single()) : ?>
 4<meta property="og:type" content="acticle" />
 5<meta property="og:description" content="<?php echo get_post_excerpt(); ?>" />
 6<meta property="og:url" content="<?php the_permalink();?>"/> 
 7<?php if ( has_post_thumbnail()):
 8  $meta_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'thumbnail'); ?>
 9<meta property="og:image" content="<?php echo $meta_image_url[0];?>" />
10<?php endif; ?>
11<?php else: ?>
12<meta property="og:type" content="website" />
13<?php endif; ?>

因为要在LOOP循环外获取文章摘要,所以自定了一个function get_post_excerpt() 在文件 functions.php 中:

 1function get_post_excerpt($post, $excerpt_length=140){
 2    if(!$post) $post = get_post();
 3    $post_excerpt = $post->post_excerpt;
 4    if($post_excerpt == ''){
 5        $post_content = $post->post_content;
 6        $post_content = do_shortcode($post_content);
 7        $post_content = wp_strip_all_tags( $post_content );
 8        $post_excerpt = mb_strimwidth($post_content,0,$excerpt_length,'…','utf-8');
 9    }
10    $post_excerpt = wp_strip_all_tags( $post_excerpt );
11    $post_excerpt = trim( preg_replace( "/[\n\r\t ]+/", ' ', $post_excerpt ), ' ' );
12    return $post_excerpt;
13}
分享

相關文章

WordPress 301重定向插件 Redirection

作者 Y Cheung1700 字4 分鐘00

朋友博客换了空间换了域名,但是又不想失去苦苦经营多年的旧博客的流量,于是想说那就设置301重定向吧。 首页和某些特定页面重定向到还好办,直接修改.htaccess就好了,就是那差不 …

启用第三方评论系统DISQUS

作者 Y Cheung3891 字8 分鐘00

在下一直很懒,没有怎么折腾过评论系统,然而每次换主题都觉得很烦,套用的别人的theme呢就要考虑是否是嵌套评论的,自己写主题呢就直接用默认主题里的评论部分,样式化设定用的还是我写的 …

添加jQuery lazy load plugin插件

作者 Y Cheung133 字1 分鐘00

有时候写日志会添加很多很大的图片,且这些图片所在图床由于我国特殊国情常常loading得非常慢。 为了提高访客的愉悦体验,特添加了这个插件,用来延迟加载图片。

启用thread twitter插件

作者 Y Cheung125 字1 分鐘00

將您的推特(Twitter)訊息(tweets),以巢狀方式在部落格的分頁呈現出來,輕鬆追蹤誰曾經回覆你的推訊,或是銳推(retweet)你的訊息, 参考 …

Blog添加Sociable插件

作者 Y Cheung99 字1 分鐘00

这个不多说,很方便的一个插件,支持世界上主流的社会化分享网站,支持自定义图标,更多选项安装后在设置中调整。 Automatically add links on your …

↑