3
1

More than 1 year has passed since last update.

【WordPress独自関数】「JSON-LD」形式の構造化データ「Article」を実装する関数

Last updated at Posted at 2022-03-01

SEO対策にもなる「JSON-LD」形式の構造化データ「Article」をWordPressに実装する方法を紹介します。
プラグインを使用すればインストールして管理画面から設定するだけで設置することも出来ますが、今回はプラグインを使わない方法を紹介したいと思います。

「JSON-LD」形式の構造化データ「Article」の記述方法

今回は「JSON-LD」形式の構造化データ「Article」の作成方法を紹介します。

<script type="application/ld+json">
  {
    "@context": "https://schema.org",
    "@type": "NewsArticle",
    "mainEntityOfPage": {
      "@type": "WebPage",
      "@id": "https://google.com/article"
    },
    "headline": "Article headline",
    "image": [
      "https://example.com/photos/1x1/photo.jpg",
      "https://example.com/photos/4x3/photo.jpg",
      "https://example.com/photos/16x9/photo.jpg"
    ],
    "datePublished": "2015-02-05T08:00:00+08:00",
    "dateModified": "2015-02-05T09:20:00+08:00",
    "author": {
      "@type": "Person",
      "name": "John Doe",
      "url": "http://example.com/profile/johndoe123"
    },
    "publisher": {
      "@type": "Organization",
      "name": "Google",
      "logo": {
        "@type": "ImageObject",
        "url": "https://google.com/logo.jpg"
      }
    }
  }
</script>

「JSON-LD」形式の構造化データ「Article」は上記のようなマークアップで記述することが可能です。
しかし、各投稿に上記のコードを登録する作業は正直面倒です。修正や更新する場合にも、各投稿に登録したコードを手動で修正しなくてはいけないため、時間がかかります。
そこで、WordPressのテーマ内のfunctions.phpに関数を追加し、wp_headにフックさせることで「JSON-LD」形式の構造化データ「Article」を自動生成し出力させたいと思います。

構造化データ「Article」の自動生成関数

下記のコードをテーマ内のfunctions.phpに記載いただければ<head>内に「JSON-LD」形式の構造化データ「Article」が自動で生成されます。
なお、下記のコードは基本コードです。実装する際には、**実装するサイトの仕様や要望にあわせてカスタマイズして利用**してください。

function json_article() {

  /** 表示ページのWPオブジェクトを取得 **/
  $post = get_queried_object();

  /** 投稿者情報を取得 **/
  $author = get_userdata( $post->post_author );
  $author_name = $author->display_name; // 表示名
  $author_url = $author->user_url; // サイト

  $permalink = ( empty($_SERVER["HTTPS"] ) ? "http://" : "https://") . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];

  /**
  * 除外設定
  **/
  if( is_admin() ) { return false; }

  /** 各種詳細 & トップページ **/
  if ( is_singular() || is_page() || is_home() || is_front_page() ) {

    if( has_post_thumbnail() ) {
      /** アイキャッチ画像 **/
      $image_url = get_the_post_thumbnail_url();
    } else {
      $image_url = '代替え画像のURL';
    }

    if( is_home() || is_front_page() ) {
      $title = get_bloginfo( 'name' );
    } else {
      $title = get_the_title() . ' | ' . get_bloginfo( 'name' );
    }

    $json_article = array(
      "@context"         => "https://schema.org",
      "@type"            => "NewsArticle",
      "mainEntityOfPage" => array(
        "@type" => "WebPage",
        "@id"   => esc_url( $permalink ),
      ),
      "headline"         => $title,
      "image"            => array(
        $image_url,
      ),
      "datePublished"    => get_the_time( 'c' ),
      "dateModified"     => get_the_modified_time( 'c' ),
      "author"           => array(
        "@type" => "Person",
        "name"  => $author_name,
        "url"   => esc_url( $author_url ),
      ),
      "publisher"        => array(
        "@context"    => "http://schema.org",
        "@type"       => "Organization",
        "name"        => get_bloginfo( 'name' ),
        "description" => get_bloginfo( 'description' ),
        "logo"        => array(
          "@type" => "ImageObject",
          "url"   => "サイトのロゴ画像のURL",
        ),
      ),
    );

    echo '<script type="application/ld+json">' . json_encode( $json_article , JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT ) . '</script>';

  }

}

add_action( 'wp_head', 'json_article' );

参考

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1