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

How do I have a fallback CSS style if no featured image is selected in WordPress? I'd rather remove the featured im

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']; ?>')">

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745362520a4624424.html

相关推荐

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

    How do I have a fallback CSS style if no featured image is selected in WordPress? I'd rather remove the featured im

    4小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信