在函数中。php:
function yourTheme_thumbnail($size) {
if( has_post_thumbnail() ) {
GLOBAL $post;
// get thumbnail url with given size.
$url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), \'$size\' );
// return it as an CSS property.
$backgroundImage = \'background-image: url(\' . $url[0] . \');\';
echo $backgroundImage;
}
}
在post循环中:
LOOP START
<a href="<?php the_permalink() ?>">
<div class="post-thumbnail" style="<?php yourTheme_thumbnail(\'medium\') ?>">
<h2 class="caption"><?php the_title() ?></h2>
</div>
</a>
LOOP END
最后,在CSS中添加以下规则:
.post-thumbnail {
background-image: url(images/bg.png);
background-color: rgba(0, 0, 0, .7);
background-repeat: no-repeat;
background-position: center center;
background-origin: fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
和bg。如果未设置缩略图,png是默认图像。