最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Add a default style if no Featured image is selected

programmeradmin1浏览0评论

How do I have a fallback CSS style if no featured image is selected in WordPress? I'd rather remove the featured image block and replace with some CSS class if nothing is selected.

<?php $thumb = wp_get_attachment_image_src( 
get_post_thumbnail_id($post->ID), 'full' );?>
<div id="post" class="featured-image" style="background-image: url('<? 
php echo $thumb['0'];?>')">

Added Featured Image

How do I have a fallback CSS style if no featured image is selected in WordPress? I'd rather remove the featured image block and replace with some CSS class if nothing is selected.

<?php $thumb = wp_get_attachment_image_src( 
get_post_thumbnail_id($post->ID), 'full' );?>
<div id="post" class="featured-image" style="background-image: url('<? 
php echo $thumb['0'];?>')">

Added Featured Image

Share Improve this question asked Jun 27, 2019 at 18:51 JthomasJthomas 112 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You can test if the $thumb has any contents and then apply a class to the featured image block that you can use to hide the container.

<?php
    $thumb = wp_get_attachment_image_src(
        get_post_thumbnail_id($post->ID),
        'full'
    );
?>

<div id="post" class="featured-image <?php echo empty($thumb) ? 'no-featured-image-class' : ''; ?>" style="background-image: url('<?php echo $thumb['0']; ?>')">

发布评论

评论列表(0)

  1. 暂无评论