WordPRESS header.php メタ og

・og:type

記事の種類。

content属性値:ブログや、ニュースの記事はarticleと指定。

------

・og:title

記事のタイトル。

<?php the_title(); ?>で記事のタイトルを出力。

------

・og:url

記事のURL。

<?php echo the_permalink(); ?>で記事の個別URLを出力。

-------

・og:description

記事の概要。

<?php echo wp_trim_words(); ?>で記事の本文から100文字を抽出して出力。

 -------

header.php

<head prefix="og: http://ogp.me/ns#">

 

// 記事の個別ページ用メタデータ

<?php if (is_single() ):  ?>

<meta property="og:type" content="article">

<meta property="og:title" content="<?php the_title(); ?>">

<meta property="og:url" content="<?php the_permalink(); ?>">

<meta property="og:description" content="<?php echo wp_trim_words($post->post_content, 100,'...'); ?>">

<?php endif; ?>

 

// wp_get_attachment_image_src()

画像に関するデータを取得し、出力。

----

// get_post_thumbnail_id()

アイキャッチ画像のURLを取得。

----

// <?php echo get_template_directory_uri(); ?> / 00.jpg">

テーマフォルダのURLを指定。

-----

<?php if(has_post_thumbnail() ): ?>

   <?php $postthumb = wp_get_attachment_image_src(get_post_thumbnail_id(), 'large'); ?>

     <meta property="og:image" content="<?php echo $postthumb[0]; ?>">

 

<?php elseif(preg_match('/wp-image-(\d+)/s', $post->post_content, $thumbid)) : ?>

   <?php $postthumb = wp_get_attachment_image_src($thumbid[1], 'large'); ?>

   <meta property="og:image" content="<?php echo$postthumb[0]>">

 

<?php else: ?>

     <meta property="og:image" content="<?php echo get_template_directory_uri(); ?>/00.jpg">

<?php endif; ?>

 <?php endif;   // 記事の個別ページ用のメタデータはここまで ?>

----

・og:site_name

サイト名。

<?php bloginfo('name'); ?> でサイト名を出力。

-----

・og:locale

言語の種類。

ja_JP と指定して日本語サイトであることを明示。

----

<meta property="og:site_name" content="<?php bloginfo('name'); ?>">

<meta property="og:locale" content="ja_JP">