我是php新手。开发WordPress网站。
我想在每个帖子/页面上添加一个特色图片作为英雄背景图片。如果没有特色图片,我想使用默认图片。
我发现有效的代码可以单独完成上述每个操作,但不确定如何将它们组合在一起。
特色图片作为背景图片:
<?php $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' );?>
<div id="post" class="hero-image" style="background-image: url('<?php echo $thumb['0'];?>')">
</div>
后备默认图片:
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail( 'full' );
} else { ?>
<img src="<?php bloginfo('template_directory'); ?>/img/default-img.jpg" alt="<?php the_title(); ?>" />
<?php } ?>
编辑:这是我的方法:
<?php if ( has_post_thumbnail() ) { ?>
<?php $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' );?>
<div class="hero-image" style="background-image: url('<?php echo $thumb['0'];?>')"></div>
<?php } else { ?>
<div class="hero-image" style="background-image: url('<?php bloginfo('template_directory'); ?>/img/default-img.jpg') "></div>
<?php } ?>
这是方法。
<figure>
<?php if (has_post_thumbnail()) { ?>
<?php echo wp_get_attachment_image(get_post_thumbnail_id($post->ID),
'full'); ?>
<?php } else { ?>
<img src="<?php echo get_stylesheet_directory_uri()
.'/components/assets/images/blog-placeholder.png'; ?>">
<?php } ?>
</figure>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句