Wordpressでリッチカードをコピペで自動生成できるようにしてみた。
SerchConsoleにリッチカードという項目があり、便利そうだったので自動生成できるようにしてみました。
ソースコード
過程なんてどうでも良いと思うのでいきなりソース貼っちゃいますw
こいつをfunctions.phpに貼り付けてください。
コピペ前に、ロゴのURLだけは入れるようにしてください!!
// JSON-LD add_action('wp_head','insert_json_ld'); function insert_json_ld (){ if (is_single()) { if (have_posts()) : while (have_posts()) : the_post(); $logoUrl = '';//サイトロゴのURL $context = 'http://schema.org'; $type = 'BlogPosting'; $name = get_the_title(); $authorType = 'Person'; $authorName = get_the_author(); $dataPublished = get_the_date('Y-n-j'); $thumbnail_id = get_post_thumbnail_id($post->ID); $image = wp_get_attachment_image_src( $thumbnail_id, 'thumbnail' ); $imageurl = $image[0]; $imageWidth = $image[1]; $imageHeight = $image[2]; $category_info = get_the_category(); $articleSection = $category_info[0]->name; $articleBody = get_the_content(); $url = get_permalink(); $publisherType = 'Organization'; $publisherName = get_bloginfo('name'); $imageType = 'ImageObject'; $dateModified = get_the_modified_date('Y-n-j'); $mainEntityOfPageType = 'WebPage'; $json= " \"@context\" : \"{$context}\", \"@type\" : \"{$type}\", \"mainEntityOfPage\" : { \"@type\" : \"{$mainEntityOfPageType}\", \"@id\" : \"{$url}\" }, \"name\" : \"{$name}\", \"headline\" : \"{$name}\", \"author\" : { \"@type\" : \"{$authorType}\", \"name\" : \"{$authorName}\" }, \"datePublished\" : \"{$dataPublished}\", \"image\" : { \"@type\" : \"{$imageType}\", \"url\" : \"{$imageurl}\", \"height\" : \"{$imageHeight}\", \"width\" : \"{$imageWidth}\" }, \"articleSection\" : \"{$articleSection}\", \"url\" : \"{$url}\", \"dateModified\" : \"{$dateModified}\", \"publisher\" : { \"@type\" : \"{$publisherType}\", \"name\" : \"{$publisherName}\", \"logo\" : { \"@type\" : \"{$imageType}\", \"url\" : \"{$logoUrl}\" } } "; echo ' '; endwhile; endif; rewind_posts(); } }
確認方法
構造化テストツールでお試しください。
エラーが出てなければOKです。
もしエラーがでるようだったらエラーに沿って修正していただければOKかと。
わからなかったらコメントください。
追記:なんか1週間くらい経っても検索結果に反映されませんでした。
なぜ?
Article(マークアップ: schema.org)検索結果表示しない
という記事の中に、
トップニュースにAMPとして掲載されるには、次のいずれかのschema.orgでのマークアップが必要です。
Article
NewsArticle
BlogPosting
VideoObject
という記載があり、もしかして「BlogPosting」じゃないとダメ??
ちょっと変更して試してみます。