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 badges1 Answer
Reset to default 0You 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
评论列表(0条)