php - 如何在 Wordpress 中将帖子特色图片设置为背景图片

标签 php html css wordpress

我创建了一个部分来显示我所有的帖子内容。我现在的努力是将帖子缩略图设置为背景图片。

这里是该部分的结构:

<section id="testimonials"> 
    <div class="testimonials-container">
        <div class="heading-container">
            <h3 class="page-title">Testimonials</h3>
        </div>
        <div class="testimonials-box">
            <?php 
                $queryposts = array(            
                    'post__in' => array(75,73), 
                    'post_type' => 'post',
                    'posts_per_page' => -1,  
                    'order' => 'ASC' 
                );
                $lastblog = new WP_Query( $queryposts );            
                if($lastblog->have_posts() ):
                    while($lastblog->have_posts()): $lastblog->the_post(); ?>

                    <div class="testimonial-wrapper">
                        <div class="testimonial-item">   
                            <p class="test-content"><?php the_content(); ?></p>
                            <p class="test-title"><?php the_title(); ?></p>
                            <?php $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' );?>
                            <div class"image-class" style="background-image: url('<?php echo $thumb['0'];?>')"></div>
                            </div>
                    </div>
                    <?php endwhile;
                endif;            
                wp_reset_postdata();
            ?> 
        </div>  
    </div>
</section>

这是我的 CSS:

section#testimonials {
    height: 400px;
    background-image: url(images/testimonials-bg.jpg);
    background-size: cover;
    background-repeat: no-repeat;
    text-align: center;

}

.testimonial-wrapper {
    width: 100%;
    max-width: 900px;
    margin: 0 auto;
}

.image-class{
    display: block;
    width: 125px;
    height: 125px;
    background-size: cover;
    background-repeat: no-repeat;
}

当我尝试查看页面时,图像未显示,并试图检查页面时显示如下:

background-image: url((unknown));

如何将缩略图设置为我的 div 框的背景图像?

希望能帮到你

最佳答案

请尝试下面的代码,

<?php $thumb = get_the_post_thumbnail_url(); ?>
<div class="image-class" style="background-image: url('<?php echo $thumb;?>')"></div>

关于php - 如何在 Wordpress 中将帖子特色图片设置为背景图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43576107/

相关文章:

php - mysql/php 中事件的基于时间的资源可用性

php 数据库帮助,数据库托管在与 Web 主机不同的服务器上

php - 具有大量列的 mysql 表的最佳方法

javascript - 滚动 html 元素仅适用于 Chrome

html - 列有问题。试图让一个带有图像的列和一个带有文本和背景的列彼此相邻

css - 如何强制浏览器在 css 文件更改时不缓存 css 文件?

javascript - 当URL包含#key-1时跳转到name=key-1

javascript - IE10 IE11 显示 "This page can' t 被显示“在大型 DOM 上 - 在 firefox 中工作

html - 如何更改可点击图像的 ol 元素符号

css - 当我将 div 向左浮动时,它消失了,为什么?