Skip to content

Instantly share code, notes, and snippets.

@TAKAyukiatkwsk
Created March 31, 2012 13:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TAKAyukiatkwsk/2264002 to your computer and use it in GitHub Desktop.
Save TAKAyukiatkwsk/2264002 to your computer and use it in GitHub Desktop.
WordPressサイトのOGP設定
<?php
function my_blog_description() {
$description = '';
if ( is_single() ) {
// 投稿の場合
$_post = get_post();
$description = $_post->post_content;
$description = preg_replace( "/[\n|\r|\n\r|\t|\s]/", '', $description );
$description = strip_tags( $description );
$more_str = mb_strlen( $description, 'UTF-8' ) > 100 ? '...' : '';
$description = htmlspecialchars( mb_substr( $description, 0, 100, 'UTF-8' ) . $more_str );
} else {
$description = bloginfo( 'description' );
}
return $description;
}
<!DOCTYPE html>
<html>
<head>
<?php
global $page, $paged;
$title = wp_title( '|', false, 'right' );
// Add the blog name.
$title .= get_bloginfo( 'name' );
// Add a page number if necessary:
if ( $paged >= 2 || $page >= 2 )
$title .= ' | ' . sprintf( __( 'Page %s', 'twentyeleven' ), max( $paged, $page ) );
?><title><?php echo $title; ?></title>
<meta property="og:title" content="<?php echo $title; ?>" />
<meta property="og:type" content="<?php echo is_home() ? 'blog' : 'article'; ?>" />
<meta property="og:url" content="<?php echo 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; ?>" />
<meta property="og:site_name" content="<?php bloginfo( 'name' ); ?>" />
<meta property="fb:admins" content="" /><!-- facebook IDを設定する -->
<meta property="og:description" content="<?php echo my_blog_description(); ?>" />
</head>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment