DEV Community

kumamon
kumamon

Posted on

2

【WordPress】アイキャッチ画像がある時、無い時の分岐OGP

<?php if(has_post_thumbnail()): ?> 
<?php 
// アイキャッチ画像のIDを取得
$thumbnail_id = get_post_thumbnail_id(); 

$eye_img = wp_get_attachment_image_src( $thumbnail_id , 'full' );

echo '<meta property="og:image" content="'.$eye_img[0].'">';
?>
<?php else: ?>
    <meta property="og:image" content="★アイキャッチ無い時の指定画像★">
<?php endif; ?>

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay