LoginSignup
2
2

More than 1 year has passed since last update.

Yoast SEO OGP画像をカスタムフィールド値で上書き

Posted at

YoastSEOではOGP画像にアイキャッチ画像が出力される

YoastSEOでは、記事ページはアイキャッチ画像が出力される仕様になっています。

YoastSEOでOGP画像を上書きする

今回は、OGP画像をアイキャッチではなく、カスタムフィールドで出力する必要がありました。

functions.phpに以下の記述をすることで、指定したページに、カスタムフィールドでOGP画像を上書きできます。

function custom_opengraph_image($img) {
	global $post;

    // Advanced Custom Fields ので設定した`sample_img`の値を取得
	$top_image = get_field('sample_img', $post->ID);

    // 投稿タイプ`sample`の詳細ページを指定
	if (is_singular(array('sample'))){
		return $top_image;
	}
	return $img;
}
add_filter('wpseo_opengraph_image', 'custom_opengraph_image');
2
2
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
2
2