イベントのエラーってなんだ?
前回の記事でEvent Organiser を使用したら、Google Search Consoleに見慣れないエラーが表示されました。
Event Organaiserの構造化データが不足しているので、Googleの検索結果にイベントが表示されない
というエラーです。必須項目さえ記述されていれば、Googleの検索結果にイベントの詳細が表示されます。
これは対応しなきゃということで、Event Organiser のコードを解析してみました。
イベント名の属性追加
エラーが出ているのはイベントのアーカイブページです。
event のname属性とlocationのaddress属性を追加すればいいので、アーカイブページのテンプレートを探します。以下のファイルが該当しました。
- wp-content\plugins\event-organiser\templates\eo-loop-single-event.php 23行目あたり
<h2 class="eo-event-title entry-title">
<a href="<?php echo eo_get_permalink(); ?>" itemprop="url">
<span itemprop="summary"><?php the_title() ?></span>
</a>
</h2>
イベントのタイトルをsummary という属性で指定しています。これを”name”に変更すれば一つ問題は片付きそう。ただし、このファイルを直接書き換えると今後プラグインが更新されるたびに変更が必要になります。
テーマフォルダに同じファイル名でコピーしてそちらで修正します。
- wp-content\themes\suehiro\eo-loop-single-event.php
<h2 class="eo-event-title entry-title">
<a href="<?php echo eo_get_permalink(); ?>" itemprop="url">
<span itemprop="name"><?php the_title() ?></span>
</a>
</h2>
location.addressの追加
この調子でlocation.addressも追加と思ったら、
- wp-content\plugins\event-organiser\templates\eo-loop-single-event.php 46行目あたり
//A list of event details: venue, categories, tags.
echo eo_get_event_meta_list();
イベント会場の詳細はeo_get_event_meta_list関数で作成しています。この関数を解析しないとできません。grepしたら、次の場所にありました。関数の最後でfilterを呼び出しているのでオリジナルのソースをいじることなく修正できそう。
- wp-content\plugins\event-organiser\includes\event-organiser-event-functions.php 1578行目あたり
/**
* Filters mark-up for the event details list.
*
* The event details list is just a simple list containig details pertaining
* to the event (venue, categories, tags) etc.
*
* @param array $html The generated mark-up
* @param int $event_id Post ID of the event
*/
$html = apply_filters( 'eventorganiser_event_meta_list', $html, $event_id );
return $html;
テーマフォルダのfunctions.phpに関数をコピーして以下のようにちょっと修正。
itemprop=”address”属性のspanブロックを追加しています。
function suehiro_event_meta_list( $html, $event_id ) {
$html = '<ul class="eo-event-meta">';
$venue = get_taxonomy( 'event-venue' );
if ( ( $venue_id = eo_get_venue( $event_id ) ) && $venue ) {
$address = eo_get_venue_address( $venue_id);
$html .= sprintf(
'<li><strong>%s:</strong> <a href="%s">
<span itemprop="location" itemscope itemtype="http://schema.org/Place">
<span itemprop="name">%s</span>
<span itemprop="geo" itemscope itemtype="http://schema.org/GeoCoordinates">
<meta itemprop="latitude" content="%f" />
<meta itemprop="longitude" content="%f" />
</span>
<span itemprop="address" itemscope itemtype="http://schema.org/PostalAddress">
<meta itemprop="postalCode" content="%s" />
<meta itemprop="addressCountry" content="%s" />
<meta itemprop="addressRegion" content="%s" />
<meta itemprop="addressLocality" content="%s" />
<meta itemprop="streetAddress" content="%s" />
</span>
</span>
</a></li>',
$venue->labels->singular_name,
eo_get_venue_link( $venue_id ),
eo_get_venue_name( $venue_id ),
eo_get_venue_lat( $venue_id ),
eo_get_venue_lng( $venue_id ),
$address['postcode'],
$address['country'],
$address['state'],
$address['city'],
$address['address']
);
}
if ( get_the_terms( $event_id, 'event-category' ) && !is_wp_error( get_the_terms( $event_id, 'event-category' ) ) ) {
$html .= sprintf(
'<li><strong>%s:</strong> %s</li>' . "\n",
__( 'Categories', 'eventorganiser' ),
get_the_term_list( $event_id, 'event-category', '', ', ', '' )
);
}
if ( get_the_terms( $event_id, 'event-tag' ) && !is_wp_error( get_the_terms( $event_id, 'event-tag' ) ) ) {
$html .= sprintf(
'<li><strong>%s:</strong> %s</li>' . "\n",
__( 'Tags', 'eventorganiser' ),
get_the_term_list( $event_id, 'event-tag', '', ', ', '' )
);
}
$html .= '</ul>';
return $html;
}
add_filter('eventorganiser_event_meta_list', 'suehiro_event_meta_list', 10, 2);
これでうまくいくかどうか、Googleのインデックス更新待ちです。
2 thoughts on “Google Search Consoleでイベントエラー”