DEV Community 👩‍💻👨‍💻

kumamon
kumamon

Posted on

【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)

DEV has this feature:

Settings

Go to your customization settings to nudge your home feed to show content more relevant to your developer experience level. 🛠