快轉到主要內容

Wordpress主题添加ogp协议meta标签

·459 字·1 分鐘
Y Cheung
作者
Y Cheung
Blogger, Programer & Traveler.

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}

相關文章

Highslide Image WP插件完成

·237 字·1 分鐘
Y Cheung的玩樂博客要搬家啦,用了N多年的插件原作者一直不更新,嗚嗚嗚,只好自己重新寫一個喇! Highslide Image - Wordpress插件 我的思路是: 編輯器上添加一個按鈕——彈出窗口——供填寫圖片URL及描述的表單——插入代碼至編輯器中——前台樣式化效果顯示。

Wordpress默认相册与文字内容分开显示及定制显示格式

·1319 字·3 分鐘
最近工作上要重新建站,公司有些站点是ASP的,程序老旧经常被黑。因为就是一般的企业展示类站点,就直接用wordpress重建了,页面样式还是用原来的,工作重点就是如何output出跟旧站一样的html结构。得益于这大半年的几个drupal项目,在做的时候思路跟以前不同了,不得不说drupal的核心思维还是很赞很科学的。

Wordpress在循环LOOP中获取 post_date 使用自定义日期时间格式

·146 字·1 分鐘
code示例: 1<?php $args = array('numberposts' => '16','category' => '8'); 2 $recent_posts = wp_get_recent_posts( $args, ARRAY_A ); 3 foreach ($recent_posts as $recent) { 4 echo '<li><a href="'. get_permalink($recent["ID"]).'">'.$recent["post_title"].'</a><span class="list-meta">'. mysql2date('Y-m-d', $recent["post_date"]) .'</span></li>'; 5 } 6?> 原本直接用$recent['post_date']输出的是2014-11-11 14:30:24这样的格式,但现在不需要时间的输出,Y.Cheung 只需要输出日期,找了一下用mysql2date函数就可以。

ubuntu vps上的文件权限配置

·238 字·1 分鐘
设置目录内所有文件及文件夹(包括子文件夹内)的权限为-rw-r–r–(644): chmod -R 644 * 设置目录内所有文件(不包括子文件夹内)的权限为-rw-r–r–(644):