この記事では、WordPressの投稿ページで使用している口コミ(カスタムフィールド)を、Googleのリッチリザルト(構造化マークアップ)に対応させ、検索結果に星付きレビューを表示させる方法を初心者でも分かるように解説します。
目次
目的
- SCF(Smart Custom Fields)で登録した口コミをもとに、
- Googleの構造化データに対応した形式(JSON-LD)で出力し、
- リッチリザルトでレビューを表示させる。
前提条件
- WordPressテーマ「SWELL」を使用している
- 口コミは「SCFのグループフィールド」で以下のように設定されている:
- テキストエリア「口コミ本文」 →
review_text
- テキスト「口コミ引用元」 →
review_source
- 数値(小数)「評価点(5点満点)」 →
review_number
口コミの評価点の項目を追加
タイプ | ラベル | 名前 | デフォルト |
テキスト | 口コミ評価 | review_number | 3.5 |
店舗ページ用カスタムフィールド設定の口コミ部分
口コミ部分をに変更する
// 修正:口コミショートコードに評価点(review_number)を追加し、星とともに表示
function generate_blockquote_shortcode( $atts ) {
global $post;
$group = SCF::get( '口コミ', $post->ID );
if ( ! is_array( $group ) || empty( $group ) ) {
return '';
}
$output = '<div class="review-boxes">';
foreach ( $group as $fields ) {
$review_text = isset( $fields['review_text'] ) ? trim( $fields['review_text'] ) : '';
$review_source = isset( $fields['review_source'] ) ? trim( $fields['review_source'] ) : '';
$review_number = isset( $fields['review_number'] ) ? floatval( $fields['review_number'] ) : null;
// review_text と review_source が必須。review_numberはあれば表示。
if ( $review_text === '' || $review_source === '' ) {
continue;
}
// 星の表示(デフォルトは非表示)
$stars_html = '';
if ( is_numeric( $review_number ) && $review_number > 0 ) {
$full = floor( $review_number );
$half = ( $review_number - $full >= 0.5 ) ? 1 : 0;
$empty = 5 - $full - $half;
for ( $i = 0; $i < $full; $i++ ) {
$stars_html .= '<span class="star full">★</span>';
}
if ( $half ) {
$stars_html .= '<span class="star half">★</span>';
}
for ( $i = 0; $i < $empty; $i++ ) {
$stars_html .= '<span class="star empty">☆</span>';
}
$stars_html = '<div class="review-score">' . $stars_html . '<span class="score"> ' . number_format( $review_number, 1 ) . '点</span></div>';
}
$output .= '<div class="custom-review-box">';
$output .= $stars_html;
$output .= '<div class="review-text">' . esc_html( $review_text ) . '</div>';
$output .= '<div class="review-source">' . esc_html( $review_source ) . '</div>';
$output .= '</div>';
}
$output .= '</div>';
return $output;
}
add_shortcode( 'custom_blockquote', 'generate_blockquote_shortcode' );
レビュー用スニペットの作成
「レビューの構造化データを出力するスニペット」のcode snipetを新規作成
add_action( 'wp_head', 'output_schema_for_localbusiness' );
function output_schema_for_localbusiness() {
if ( ! is_singular( 'post' ) ) return;
global $post;
$salon_name = SCF::get( 'salon_name', $post->ID );
$address = SCF::get( 'address', $post->ID );
$reviews = SCF::get( '口コミ', $post->ID );
if ( empty( $salon_name ) || empty( $address ) || empty( $reviews ) ) return;
$review_count = 0;
$score_total = 0;
$review_jsons = [];
foreach ( $reviews as $r ) {
if ( empty( $r['review_text'] ) || empty( $r['review_source'] ) || empty( $r['review_number'] ) ) continue;
$score_total += floatval( $r['review_number'] );
$review_count++;
$review_jsons[] = [
"@type" => "Review",
"author" => $r['review_source'],
"reviewBody" => $r['review_text'],
"reviewRating" => [
"@type" => "Rating",
"ratingValue" => number_format( $r['review_number'], 1 ),
"bestRating" => "5"
]
];
}
if ( $review_count === 0 ) return;
$average = $score_total / $review_count;
$data = [
"@context" => "https://schema.org",
"@type" => "LocalBusiness",
"name" => $salon_name,
"address" => [
"@type" => "PostalAddress",
"streetAddress" => $address
],
"aggregateRating" => [
"@type" => "AggregateRating",
"ratingValue" => number_format( $average, 1 ),
"reviewCount" => $review_count
],
"review" => $review_jsons
];
echo '<script type="application/ld+json">' . wp_json_encode( $data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT ) . '</script>';
}
CSS Editorに追加
以下の部分をCSS Editorの末尾に追加
/* 口コミ分追加 */
.review-boxes {
display: grid;
gap: 1.5em;
margin: 2em 0;
}
.custom-review-box {
border: 1px solid #ccc;
padding: 1em 1.2em;
background-color: #fdfdfd;
border-radius: 8px;
box-shadow: 0 2px 5px rgba(0,0,0,0.05);
}
.review-score {
font-size: 1.2em;
font-weight: bold;
margin-bottom: 0.4em;
}
.review-score .star.full {
color: #FBDD63;
}
.review-score .star.empty {
color: #E9E9E9;
}
.review-score .score {
margin-left: 0.5em;
color: #333;
font-size: 0.95em;
}
.review-text {
font-size: 1.05em;
margin-bottom: 0.5em;
color: #333;
}
.review-source {
text-align: right;
font-size: 0.85em;
color: #666;
}
.review-score .star.half {
position: relative;
display: inline-block;
color: #E9E9E9; /* ベース色(グレー) */
}
.review-score .star.half::before {
content: '★';
position: absolute;
left: 0;
width: 50%;
overflow: hidden;
color: #FBDD63; /* 半分塗りつぶし色 */
}
コメント