Last active
September 20, 2018 00:27
-
-
Save ikekou/c74404e0719f6c63e4a6 to your computer and use it in GitHub Desktop.
[WordPress] OGP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- ここからOGP --> | |
<meta property="og:type" content="blog"> | |
<?php | |
if (is_single()){//単一記事ページの場合 | |
if(have_posts()): while(have_posts()): the_post(); | |
echo '<meta property="og:description" content="'.get_post_meta($post->ID, _aioseop_description, true).'">';echo "\n";//抜粋を表示 | |
endwhile; endif; | |
echo '<meta property="og:title" content="'; the_title(); echo '">';echo "\n";//単一記事タイトルを表示 | |
echo '<meta property="og:url" content="'; the_permalink(); echo '">';echo "\n";//単一記事URLを表示 | |
} else {//単一記事ページページ以外の場合(アーカイブページやホームなど) | |
echo '<meta property="og:description" content="'; bloginfo('description'); echo '">';echo "\n";//「一般設定」管理画面で指定したブログの説明文を表示 | |
echo '<meta property="og:title" content="'; bloginfo('name'); echo '">';echo "\n";//「一般設定」管理画面で指定したブログのタイトルを表示 | |
echo '<meta property="og:url" content="'; bloginfo('url'); echo '">';echo "\n";//「一般設定」管理画面で指定したブログのURLを表示 | |
} | |
$str = $post->post_content; | |
$searchPattern = '/<img.*?src=(["\'])(.+?)\1.*?>/i';//投稿にイメージがあるか調べる | |
if (is_single()){//単一記事ページの場合 | |
if (has_post_thumbnail()){//投稿にサムネイルがある場合の処理 | |
$image_id = get_post_thumbnail_id(); | |
$image = wp_get_attachment_image_src( $image_id, 'full'); | |
echo '<meta property="og:image" content="'.$image[0].'">';echo "\n"; | |
} else if ( preg_match( $searchPattern, $str, $imgurl ) && !is_archive()) {//投稿にサムネイルは無いが画像がある場合の処理 | |
echo '<meta property="og:image" content="'.$imgurl[2].'">';echo "\n"; | |
} else {//投稿にサムネイルも画像も無い場合の処理 | |
echo '<meta property="og:image" content="http://ikekou.jp/dogear/wp-content/uploads/2015/03/dogear-ogimage.png">';echo "\n"; | |
} | |
} else {//単一記事ページページ以外の場合(アーカイブページやホームなど) | |
echo '<meta property="og:image" content="http://ikekou.jp/dogear/wp-content/uploads/2015/03/dogear-ogimage.png">';echo "\n"; | |
} | |
?> | |
<meta property="og:site_name" content="<?php bloginfo('name'); ?>"> | |
<meta property="fb:admins" content="100000071410358"> | |
<!-- ここまでOGP --> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment