イベントのタイトルにカテゴリー名を追加したい

まちゼミ久が原で検索しても各講座のページが表示されない

前回の続きでSEO対策です。

Googleで「まちゼミ久が原」で検索しても、各講座のページが検索結果に表示されません。
これじゃまずい、講座の会場と第3回まちゼミ久が原をtitleタグに追加したい!

All in One SEOプラグイン

All in One SEOプラグインを使用しているので、設定をちょっと変更すればすぐできるはず!と
簡単に考えていましたが、ハマりました。

どうやら、カスタムタクソノミーをマクロに使えるのはPRO版(有料)だけみたいです。
以下のようにヒントには出てくるのですが、指定してもマクロを置き換えてくれません。

レッツ解析

素直にお金払う手もありますが、ソースを解析してみました。
’%tax_’でgrepすると以下の関数で処理していました。

  • wp-content\plugins\all-in-one-seo-pack\aioseop_class.php 1201行目付近

get_preview_snippet_title()関数

			if ( AIOSEOPPRO ) {
				if ( strpos( $title_format, '%tax_', 0 ) && ! empty( $post ) ) {
					$taxes = get_object_taxonomies( $post, 'objects' );
					if ( ! empty( $taxes ) ) {
						foreach ( $taxes as $t ) {
							if ( strpos( $title_format, "%tax_{$t->name}%", 0 ) ) {
								$terms = $this->get_all_terms( $post->ID, $t->name );
								$term  = '';
								if ( count( $terms ) > 0 ) {
									$term = $terms[0];
								}
								$title_format = str_replace( "%tax_{$t->name}%", $term, $title_format );
							}
						}
					}
				}
			}

はい、AIOSEOPPROが有効かどうかでガードしてますね。
でも、その下の方をみたら、フィルターフックがありました。ラッキー。

		/**
		 * The aioseop_title_format filter hook.
		 *
		 * Filter the title for the preview snippet after replacing all macros.
		 *
		 * @since 3.0.0
		 *
		 * @param string $title_format Title format to be filtered.
		 */
		$title_format = apply_filters( 'aioseop_title_format', $title_format );

テーマのfunctions.phpに’aioseop_title_format’フィルター追加すればどうにかなりそう。

フィルター追加

テーマフォルダのfunctions.phpに以下のようにフィルターを追加しました。

function suehiro_title_format($title_format) {
	global $post;
	if ( strpos( $title_format, '%tax_', 0 ) && ! empty( $post ) ) {
		$taxes = get_object_taxonomies( $post, 'objects' );
		if ( ! empty( $taxes ) ) {
			foreach ( $taxes as $t ) {
				if ( strpos( $title_format, "%tax_{$t->name}%", 0 ) ) {
					$terms = get_the_terms( $post->ID, $t->name );
					$term  = '';
					if ( count( $terms ) > 0 ) {
						$term = $terms[0]->name;
					}
					$title_format = str_replace( "%tax_{$t->name}%", $term, $title_format );
				}
			}
		}
	}
	return $title_format;
}
add_filter( "aioseop_title_format", "suehiro_title_format", 10, 1);

これでイベントカテゴリーが出力できるようになりました。
実はちょっとおかしい所もあります、なぜか縦棒が2本出る。でもいいや。(笑)

<title>14楽しく走ろう!久が原ランニング | |  ディードットステーション 久が原教室 | 第3回まちゼミ久が原</title>

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

アップロードファイルの最大サイズ: 8 MB。 添付可能なファイル:画像, 音声, 動画, 文書, スプレッドシート, 対話型, アーカイブ, その他 Youtube、Facebook、Twitter および他サービスへのリンクは自動的にコメント内に埋め込まれます。 ここにファイルをドロップ

Translate »